hibernate 多對一關係映射

實體是Employee和Department,它們之間是多對一的關係。

Department類:
Java代碼  收藏代碼
  1. public class Department {  
  2.     private int id;  
  3.     private String name;  
  4.   
  5.     public Department() {  
  6.     }  
  7.     public Department(String name) {  
  8.         this.name = name;  
  9.     }  
  10.     // getters and setters are omitted  
  11. }  


Employee類:
Java代碼  收藏代碼
  1. public class Employee {  
  2.     private int id;  
  3.     private String name;  
  4.     private Department department;  
  5.   
  6.     public Employee() {  
  7.     }  
  8.     public Employee(String name) {  
  9.         this.name = name;  
  10.     }  
  11.     // getters and setters are omitted  


Department.hbm.xml:
Xml代碼  收藏代碼
  1. <hibernate-mapping  
  2.     package="com.john.myhibernate.domain">  
  3.   
  4.     <class name="Department">  
  5.         <id name="id">  
  6.             <generator class="native"/>  
  7.         </id>  
  8.         <property name="name" length="20" not-null="true"/>  
  9.     </class>  
  10. </hibernate-mapping>  


Employee.hbm.xml:
Xml代碼  收藏代碼
  1. <hibernate-mapping package="com.john.myhibernate.domain">  
  2.   
  3. <class name="Employee">  
  4.     <id name="id">  
  5.         <generator class="native"/>  
  6.     </id>  
  7.     <property name="name" length="20" not-null="true"/>  
  8.     <many-to-one name="department" column="department_id" class="Department" fetch="select"/>  
  9. </class>  
  10. </hibernate-mapping>  

many-to-one沒有inverse屬性,因爲關係的維護是many的一方,不可能放棄對關係的維護。
many-to-one的lazy屬性有三個取值:false, proxy, no-proxy。

1. 測試cascade屬性:
Java代碼  收藏代碼
  1. public void testSaveCascade() {  
  2.     Session s = null;  
  3.     Transaction tx = null;  
  4.       
  5.     Department depart = new Department();  
  6.     depart.setName("FCI");  
  7.       
  8.     Employee em1 = new Employee("John");  
  9.     em1.setDepartment(depart);  
  10.       
  11.     Employee em2 = new Employee("Lucy");  
  12.     em2.setDepartment(depart);  
  13.       
  14.     try {  
  15.         s = HibernateUtil.getSession();  
  16.         tx = s.beginTransaction();  
  17.         s.save(em1);  
  18.         s.save(em2);  
  19.         tx.commit();  
  20.     } catch (HibernateException e) {  
  21.         tx.rollback();  
  22.         e.printStackTrace();  
  23.     } finally {  
  24.         if (s != null)  
  25.             s.close();  
  26.     }  
  27. }  

結果是報org.hibernate.TransientObjectException異常,因爲沒有保存Department實例。

可以加cascade屬性,解決問題:
Xml代碼  收藏代碼
  1. <many-to-one name="department" column="department_id" class="Department" fetch="select" cascade="save-update"/>  


2. 測試fetch
Java代碼  收藏代碼
  1. Session s = null;  
  2.   
  3. s = HibernateUtil.getSession();  
  4. Employee em = (Employee) s.get(Employee.class2);  
  5. System.out.println(em.getName());  
  6. System.out.println(em.getDepartment());  

查詢語句如下:
Hibernate: select employee0_.id as id1_0_, employee0_.name as name1_0_, employee0_.department as department1_0_, employee0_.skill as skill1_0_, employee0_.sell as sell1_0_, employee0_.type as type1_0_ from Employee employee0_ where employee0_.id=?

Hibernate: select department0_.id as id0_0_, department0_.name as name0_0_ from Department department0_ where department0_.id=?
因爲fetch設置爲select,所以對每個實體,都分別用一個SELECT語句

如果把fetch設置爲join,也就是連表查詢,只使用一個SELECT語句。如下:
Hibernate: select employee0_.id as id1_1_, employee0_.name as name1_1_, employee0_.department as department1_1_, employee0_.skill as skill1_1_, employee0_.sell as sell1_1_, employee0_.type as type1_1_, department1_.id as id0_0_, department1_.name as name0_0_ from Employee employee0_ left outer join Department department1_ on employee0_.department=department1_.id where employee0_.id=?

3. 測試lazy
當fetch爲select時,設置lazy爲proxy或者no-proxy。
Xml代碼  收藏代碼
  1. <many-to-one name="department" column="department_id" class="Department" fetch="select" cascade="save-update" lazy="no-proxy"/>  

Java代碼  收藏代碼
  1. Session s = null;  
  2.   
  3. s = HibernateUtil.getSession();  
  4. Employee em = (Employee) s.get(Employee.class2);  
  5. s.close();  
  6. System.out.println(em.getName());  
  7. System.out.println(em.getDepartment());  

結果是報org.hibernate.LazyInitializationException異常。
因爲fetch爲select,而且lazy爲proxy或者no-proxy,所以開始僅僅查詢Employee,當需要用SELECT語句查詢Department時,Session已經關閉。

解決辦法:
1. 設置lazy爲false,hibernate會第一時間把Employee和Department查詢出來。
   如果fetch爲select,使用兩個SELECT查詢語句。
   如果fetch爲join,使用一個SELECT連表查詢語句。
2. 設置fetch爲join,這時不管lazy的取值,hibernate會進行連表查詢,把兩個實體都查詢出來。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章