hibernate基础实例

hibernate基础实例:

  1. package com.javamodel.hibernate;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.Properties;  
  6.   
  7. import net.sf.hibernate.HibernateException;  
  8. import net.sf.hibernate.MappingException;  
  9. import net.sf.hibernate.Session;  
  10. import net.sf.hibernate.SessionFactory;  
  11. import net.sf.hibernate.Transaction;  
  12. import net.sf.hibernate.cfg.Configuration;  
  13.   
  14. public class Example{  
  15.       
  16.     private static SessionFactory _sessions = null;  
  17.     private static Properties pops = new Properties();  
  18.     static{  
  19.         try {  
  20.             InputStream stream = Example.class.getResourceAsStream("hibernate.properties");  
  21.             try {  
  22.                 pops.load(stream);  
  23.             } catch (IOException e1) {  
  24.                 e1.printStackTrace();  
  25.             }  
  26.             Configuration cfg = new Configuration();  
  27.             cfg.addClass(Person.class);  
  28.             cfg.setProperties(pops);  
  29.             _sessions = cfg.buildSessionFactory();  
  30.         } catch (MappingException e) {  
  31.            e.printStackTrace();  
  32.         } catch (HibernateException e) {  
  33.            e.printStackTrace();  
  34.         }  
  35.       
  36.     }  
  37.       
  38.     public static void main(String[] args) throws HibernateException {  
  39.           
  40.         Person person = new Person();  
  41.         person.setName("smallduzi");  
  42.         person.setEmail("smallduzi@sohu.com");  
  43.           
  44.         Session session = _sessions.openSession();  
  45.           
  46.         Transaction tx = null;  
  47.         try{  
  48.             tx = session.beginTransaction();  
  49.             session.save(person);  
  50.             tx.commit();          
  51.         }catch(HibernateException he){  
  52.             if(tx != null) tx.rollback();  
  53.             throw he;  
  54.         }  
  55.         finally{  
  56.             session.close();  
  57.         }  
  58.           
  59.     }  
  60.       
  61. }  
<script type="text/javascript">render_code();</script>
Person.java
代码
  1. package com.javamodel.hibernate;  
  2.   
  3. public class Person {  
  4.       
  5.     private String id = null;  
  6.     private String name = null;  
  7.     private String email = null;      
  8.       
  9.     public Person(){}  
  10.   
  11.     /** 
  12.      * @return 
  13.      */  
  14.     public String getEmail() {  
  15.         return email;  
  16.     }  
  17.   
  18.     /** 
  19.      * @return 
  20.      */  
  21.     public String getId() {  
  22.         return id;  
  23.     }  
  24.   
  25.     /** 
  26.      * @return 
  27.      */  
  28.     public String getName() {  
  29.         return name;  
  30.     }  
  31.   
  32.     /** 
  33.      * @param string 
  34.      */  
  35.     public void setEmail(String string) {  
  36.         email = string;  
  37.     }  
  38.   
  39.     /** 
  40.      * @param string 
  41.      */  
  42.     public void setId(String string) {  
  43.         id = string;  
  44.     }  
  45.   
  46.     /** 
  47.      * @param string 
  48.      */  
  49.     public void setName(String string) {  
  50.         name = string;  
  51.     }  
  52.   
  53. }  
<script type="text/javascript">render_code();</script>
Person.hbm.xml
代码
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >  
  3. <hibernate-mapping>  
  4.     <class name="com.javamodel.hibernate.Person" table="person">  
  5.         <id name="id">  
  6.             <column name="id" length="40"/>  
  7.             <generator class="uuid.hex"/>  
  8.         </id>  
  9.         <property name="name" column="name" />  
  10.         <property name="email" column="email" />  
  11.     </class>  
  12. </hibernate-mapping>  
<script type="text/javascript">render_code();</script>
hibernate.properties
代码
  1. ## Oracle  
  2.   
  3. hibernate.dialect net.sf.hibernate.dialect.OracleDialect  
  4. hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver  
  5. hibernate.connection.username XXX  
  6. hibernate.connection.password XXX  
  7. #hibernate.connection.url jdbc:oracle:thin:@192.168.0.28:1521:orcl  
  8. hibernate.connection.url jdbc:oracle:oci8:@XXX 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值