Hibernate聯合主鍵映射

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   使用   sp   java   strong   

1.聯合主鍵的映射規則

1) 類中的每個主鍵屬性都對應到資料表中的每個主鍵列。

Hibernate要求具有聯合主鍵的實體類實現Serializable介面,並且重寫hashCode與equals方法,重寫這兩個方法的原因在於Hibernate要根據資料庫的聯合主鍵來判斷某兩行記錄是否是一樣的,如果一樣那麼就認為是同一個對象,如果不一樣,那麼就認為是不同的對象。這反映到程式領域中就是根據hashCode與equals方法來判斷某兩個對象是否能夠放到諸如Set這樣的集合當中。聯合主鍵的實體類實現Serializable介面的原因在於使用get或load方法的時候需要先構建出來該實體的對象,並且將查詢依據(聯合主鍵)設定進去,然後作為get或load方法的第二個參數傳進去即可。

2) 將主鍵所對應屬性提取出一個類(稱之為主鍵類),並且主鍵類需要實現Serializable介面,重寫equals方法與hashCode方法,原因與上面一樣。

以Student類為例,實現上述兩種映射聯合主鍵的配置:

2.Student中的兩個屬性作為聯合主鍵屬性

Student類:

 
  1. public class Student implements Serializable {//必須要實現Serializable介面  
  2.   
  3.     private String cardID;//cardID和name映射為聯合主鍵  
  4.     private String name;  
  5.     private int age;  
  6.         //get、set、hashCode、equals方法省略  
  7. }  

註:可使用MyEclipse中的Sourse-->Gennerate hashCode and equals來使用MyEclipse快速產生hashCode和equals方法

 

Student.hbm.xml配置:

 
  1. <class name="bean.Student" table="student">  
  2.         <composite-id><!--聯合主鍵,student表中的主鍵為(student_name,card_id)-->  
  3.            <key-property name="name" column="student_name" type="string"></key-property><!--name及cardID為Student類中的屬性-->  
  4.            <key-property name="cardID" column="card_id" type="string"></key-property>  
  5.         </composite-id>  
  6.         <property name="age" column="student_age" type="int"></property>  
  7. </class>  

儲存對象:

 
  1. tx=session.beginTransaction();  
  2.   
  3. Student s1=new Student();  
  4. s1.setName("lisi");  
  5. s1.setAge(22);  
  6. s1.setCardID("711");  
  7. System.out.println(s1);  
  8. session.save(s1);  
  9. tx.commit();  

注意:主鍵為(card_id,student_id)若連續執行上述的儲存語句兩次,當然會拋異常,應為主鍵重複:   
           org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
3.將Student類中的兩個主鍵屬性提取為一個新的類PrimaryKey,即主鍵類

 

主鍵類PrimaryKey:

 
  1. public class PrimaryKey implements Serializable{  
  2.   
  3.     private String cardID;  
  4.     private String name;  
  5.     //get、set、hashCode、equals方法省略  
  6. }  

Student類中含有PrimaryKey類型的屬性及對應set、get方法:

 
  1. public class Student  {  
  2.     private int age;  
  3.     private PrimaryKey primaryKey;  
  4.         //set、get方法省略  
  5. }  

Student.hbm.xml檔案中的配置:

 

 
  1. <class name="bean.Student" table="student">  
  2.     <composite-id name="primaryKey" class="bean.PrimaryKey"><!--PrimaryKey為我們自訂的主鍵類-->  
  3.        <key-property name="name" column="student_name" type="string"></key-property><!--name及cardID為PrimaryKey類中的屬性-->  
  4.        <key-property name="cardID" column="card_id" type="string"></key-property>  
  5.     </composite-id>  
  6.     <property name="age" column="student_age" type="int"></property>  
  7. </class>  

儲存對象:

 
  1. tx=session.beginTransaction();  
  2.   
  3.  Student s1=new Student();  
  4.  s1.setAge(23);  
  5.  PrimaryKey p=new PrimaryKey();  
  6.  p.setCardID("102");  
  7.  p.setName("zhangsan");  
  8.  s1.setPrimaryKey(p);  
  9.  session.save(s1);  
  10.  tx.commit();  

同樣,對於上述代碼的重複執行也會導致主鍵重複拋出異常。

查詢:

 

 
  1. PrimaryKey p=new PrimaryKey();  
  2. p.setCardID("711");  
  3. p.setName("lisi");  
  4. Student s=(Student)session.get(Student.class,p);//所以PrimaryKey要實現Serializable介面  
  5. System.out.println(s.getAge());  

 

4.對於以上兩種映射聯合主鍵的方式,反映到資料庫中的表的結構是相同的,student表的內容為:

 

 

轉載請註明出處:http://blog.csdn.net/jialinqiang/article/details/8704538

Hibernate聯合主鍵映射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.