1. 程式人生 > >hibernate4的經典總結

hibernate4的經典總結

Hibernate4 環境搭建:

匯入必須的Hibernate4  JAR包
${hibernate_home}\hibernate-release-4.0.0.Final\lib\required\*

資料庫驅動包(Oracle)
ojdbc14.jar

快取(可選)

${hibernate_home}\hibernate-release-4.0.0.Final\lib\optional\ehcache\*
${hibernate_home}\hibernate-distribution-3.6.0.Final\lib\required\slf4j-api-1.6.1.jar

C3P0連線池(可選)

${hibernate_home\hibernate-release-4.0.0.Final\lib\optional\c3p0\*

編寫hibernate.cfg.xml

Xml程式碼  收藏程式碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
      
  5. <hibernate-configuration>  
  6.     <session-factory>  
  7.         <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
  8.         <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>  
  9.         <
    property name="hibernate.connection.username">scott</property>  
  10.         <property name="hibernate.connection.password">tiger</property>  
  11.         <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>  
  12.         <property name="hibernate.show_sql">true</property>  
  13.         <property name="hibernate.format_sql">true</property>  
  14.         <property name="hibernate.hbm2ddl.auto">none</property>  
  15.         <property name="hibernate.jdbc.fetch_size">100</property>  
  16.         <property name="hibernate.jdbc.batch_size">30</property>  
  17.         <!-- 配置二級快取 -->  
  18.         <property name="hibernate.cache.use_second_level_cache">true</property>  
  19.         <property name="hibernate.cache.use_query_cache">true</property>  
  20.         <!-- Hibernate4 這裡和Hibernate3不一樣 要特別注意!!!-->  
  21.         <property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>  
  22.         <!-- Hibernate3 -->  
  23.         <!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> -->  
  24.         <!-- 配置C3P0 -->  
  25.         <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>  
  26.         <property name="hibernate.c3p0.max_size">10</property>  
  27.         <property name="hibernate.c3p0.min_size">1</property>  
  28.         <property name="hibernate.c3p0.max_statements">3</property>  
  29.         <property name="hibernate.c3p0.timeout">30</property>  
  30.         <property name="hibernate.c3p0.acquire_increment">1</property>  
  31.         <property name="hibernate.c3p0.idle_test_periodt">10</property>  
  32.         <mapping resource="org/springfuncs/domain/Customer.hbm.xml" />  
  33.         <mapping resource="org/springfuncs/domain/Order.hbm.xml" />  
  34.         <mapping resource="org/springfuncs/domain/Emp.hbm.xml" />  
  35.     </session-factory>  
  36. </hibernate-configuration>  

實體類和對映檔案(沒用註解...)

Java程式碼  收藏程式碼
  1. package org.springfuncs.domain;  
  2. import java.io.Serializable;  
  3. import java.util.Date;  
  4. /** 僱員 */  
  5. public class Emp implements Serializable {  
  6.     private static final long serialVersionUID = 2127136225587213245L;  
  7.     private Integer empno; // 僱員編號  
  8.     private String ename; // 僱員姓名  
  9.     private String job; // 工作  
  10.     private Integer mgr; // 上級僱員的編號  
  11.     private Date hiredate; // 入職日期  
  12.     private Double sal; // 薪水  
  13.     private Double comm; // 獎金  
  14.     private Integer deptno; // 部門編號  
  15.     public Emp() {  
  16.     }  
  17.     public Emp(Integer empno, String ename, Date hiredate, Double sal) {  
  18.         this.empno = empno;  
  19.         this.ename = ename;  
  20.         this.hiredate = hiredate;  
  21.         this.sal = sal;  
  22.     }  
  23.     // getter and setter  
  24.     public String toString() {  
  25.         return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job  
  26.                 + ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal=" + sal  
  27.                 + ", comm=" + comm + ", deptno=" + deptno + "]";  
  28.     }  
  29. }  
Xml程式碼  收藏程式碼
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <hibernate-mapping package="org.springfuncs.domain">  
  5.     <class name="Emp" table="EMP">  
  6.         <cache usage="read-write" /> <!-- 快取 -->  
  7.         <id name="empno" column="EMPNO" length="4" type="integer">  
  8.             <generator class="assigned" />  
  9.         </id>  
  10.         <property name="ename" column="ENAME" length="10" type="string" />  
  11.         <property name="job" column="JOB" length="9" type="string" />  
  12.         <property name="mgr" column="MGR" length="4" type="integer" />  
  13.         <property name="hiredate" column="HIREDATE" type="date" />  
  14.         <property name="sal" column="SAL" type="double" />  
  15.         <property name="comm" column="COMM" type="double" />  
  16.         <property name="deptno" column="DEPTNO" length="2" type="integer" />  
  17.     </class>  
  18. </hibernate-mapping>  
Java程式碼  收藏程式碼
  1. package org.springfuncs.domain;  
  2. import java.io.Serializable;  
  3. import java.util.HashSet;  
  4. import java.util.Set;  
  5. /** 客戶 */  
  6. public class Customer implements Serializable {  
  7.     private static final long serialVersionUID = -6514738924858633212L;  
  8.     private Integer id; //id  
  9.     private String name; //姓名  
  10.     private Set<Order> orders = new HashSet<Order>(); //其下訂單  
  11.     public Customer() {  
  12.     }  
  13.     // getter and setter  
  14.     @Override  
  15.     public String toString() {  
  16.         return "Customer [id=" + id + ", name=" + name + "]";  
  17.     }  
  18. }  
Xml程式碼  收藏程式碼