Hibernate註解映射聯合主鍵的三種主要方式(轉載)

來源:互聯網
上載者:User

標籤:keyword   java類   編號   ota   code   cti   size   software   hibernate   

Hibernate註解映射聯合主鍵的三種主要方式(轉載)

轉自:http://blog.csdn.net/robinpipi/article/details/7655388

聯合主鍵用hibernate註解映射方式主要有三種:
第一、將聯合主鍵的欄位單獨放在一個類中,該類需要實現Java.io.Serializable介面並重寫equals和hascode,再將該類註解為@Embeddable,最後在主類中(該類不包含聯合主鍵類中的欄位)儲存該聯合主鍵類的一個引用,並產生set和get方法,並將該引用註解為@Id

第二、將聯合主鍵的欄位單獨放在一個類中,該類需要實現java.io.Serializable介面並重寫equals和hascode,最後在主類中(該類不包含聯合主鍵類中的欄位)儲存該聯合主鍵類的一個引用,並產生set和get方法,並將該引用註解為@EmbeddedId

第三、將聯合主鍵的欄位單獨放在一個類中,該類需要實現java.io.Serializable介面並要重寫equals和hashcode.最後在主類中(該類包含聯合主鍵類中的欄位)將聯合主鍵欄位都註解為@Id,並在該類上方將上這樣的註解:@IdClass(聯合主鍵類.class)

 

轉自:http://blog.sina.com.cn/s/blog_8297f0d00101012c.html

 

使用方式:

1、model類:

 

  1. @Entity  
  2. @Table(name="JLEE01")  
  3. public class Jlee01 implements Serializable{  
  4.   
  5.     private static final long serialVersionUID = 3524215936351012384L;  
  6.     private String address ;  
  7.     private int age ;  
  8.     private String email ;  
  9.     private String phone ;  
  10. @Id
  11.     private JleeKey01 jleeKey ; 

主鍵類:JleeKey01.java

 

 

  1. @Embeddable  
  2. public class JleeKey01  implements Serializable{  
  3.   
  4.     private static final long serialVersionUID = -3304319243957837925L;  
  5.     private long id ;  
  6.     private String name ;  
  7.     /** 
  8.      * @return the id 
  9.      */  
  10.     public long getId() {  
  11.         return id;  
  12.     }  
  13.     /** 
  14.      * @param id the id to set 
  15.      */  
  16.     public void setId(long id) {  
  17.         this.id = id;  
  18.     }  
  19.     /** 
  20.      * @return the name 
  21.      */  
  22.     public String getName() {  
  23.         return name;  
  24.     }  
  25.     /** 
  26.      * @param name the name to set 
  27.      */  
  28.     public void setName(String name) {  
  29.         this.name = name;  
  30.     }  
  31.   
  32.     @Override  
  33.     public boolean equals(Object o) {  
  34.         if(o instanceof JleeKey01){  
  35.             JleeKey01 key = (JleeKey01)o ;  
  36.             if(this.id == key.getId() && this.name.equals(key.getName())){  
  37.                 return true ;  
  38.             }  
  39.         }  
  40.         return false ;  
  41.     }  
  42.       
  43.     @Override  
  44.     public int hashCode() {  
  45.         return this.name.hashCode();  
  46.     }  
  47.       
  48. }  

2、model類:

 

 

  1. @Entity  
  2. @Table(name="JLEE02")  
  3. public class Jlee02 {  
  4.   
  5.     private String address ;  
  6.     private int age ;  
  7.     private String email ;  
  8.     private String phone ;  
  9.     @EmbeddedId
  10.     private JleeKey02 jleeKey ;  

 

主鍵類:JleeKey02.java

普通java類即可。

3、model類:

 

  1. @Entity  
  2. @Table(name="JLEE03")  
  3. @IdClass(JleeKey03.class)  
  4. public class Jlee03 {  
  5.     @Id
  6.     private long id ;
  7.     @Id  
  8.     private String name ;  

主鍵類:JleeKey03.java

 

普通java類。

主鍵類都需要實現重寫equals和hascode方法,至於是否需要實現implements Serializable介面,測試了下,貌似可以不需要。

轉自:http://laodaobazi.iteye.com/blog/903236

 

前端如果用easyui的 話,

需要這樣寫:

 {field:‘joinKey.statistic_id‘,title:‘編號‘,width:80,sortable:true,align:‘center‘,
            formatter:function(value,rowData,rowIndex){
                    
                 return rowData.joinKey.statistic_id;
                  
              }},

 {field:‘joinKey.month‘,title:‘時間(單位月)‘,width:200,sortable:true,align:‘center‘,
            formatter:function(value,rowData,rowIndex){
                    
                 return rowData.joinKey.month;
                  
              }}

joinKey為主鍵類,statistic_id、month為聯合主鍵的欄位。

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.