Hibernate @IdClass @EmbeddedID相關註解

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   os   sp   for   

Hibernate Annotations 

Mapping composite primary keys and foreign keys to composite primary keys: 

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e2177

引用Composite primary keys use a embedded class as the primary key representation, so you‘d use the @Id and @Embeddable annotations. Alternatively, you can use the @EmbeddedId annotation. Note that the dependent class has to be serializable and implements equals()/hashCode(). You can also use @IdClass.

@Entitypublic class RegionalArticle implements Serializable {    @Id    public RegionalArticlePk getPk() { ... }}@Embeddablepublic class RegionalArticlePk implements Serializable { ... }       
or alternatively
@Entitypublic class RegionalArticle implements Serializable {    @EmbeddedId    public RegionalArticlePk getPk() { ... }}public class RegionalArticlePk implements Serializable { ... }

hibernate的annotation的文檔中提供了三種方法 
  1 將組件類註解為@Embeddable,並將組件的屬性註解為@Id 
  2 將組件的屬性註解為@EmbeddedId (方便) 
  3 將類註解為@IdClass,並將該實體中所有屬於主鍵的屬性都註解為@Id(符合編程習慣)
主鍵類需要序列化(考慮到可能會將資料讀寫到虛擬記憶體中),需要重寫hashcode()和equals()方法,因為要對聯合主鍵進行比較. 

Hibernate Annotation 聯合主鍵三種寫法的例子: 
http://laodaobazi.iteye.com/blog/903236 
Which anotation should I use: @IdClass or @EmbeddedId: 
http://stackoverflow.com/questions/212350/which-anotation-should-i-use-idclass-or-embeddedid 

Compound Primary Keys with Hibernate and JPA Annotations: 
http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=15usingcompoundprimarykeys 

@IdClass and @EmbeddedId: 
http://www.coderanch.com/t/452567/ORM/java/IdClass-EmbeddedId 

Hibernate @IdClass @EmbeddedID相關註解

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.