码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate @IdClass @EmbeddedID相关注解

时间:2014-12-17 16:35:02      阅读:311      评论:0      收藏:0      [点我收藏+]

标签: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.

@Entity
public class RegionalArticle implements Serializable {

    @Id
    public RegionalArticlePk getPk() { ... }
}

@Embeddable
public class RegionalArticlePk implements Serializable { ... }       
or alternatively
@Entity
public 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相关注解

标签:style   blog   http   ar   io   color   os   sp   for   

原文地址:http://blog.csdn.net/love_xsq/article/details/41981511

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!