专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > VSTS

Hibernate注脚: 联合主键:@IdClass vs @EmbeddedId

发布时间:2011-06-20 18:22:20 文章来源:www.iduyao.cn 采编人员:星星草
Hibernate注解: 联合主键:@IdClass vs @EmbeddedId
Hibernate Annotations -> 2.2.6. 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 { ... }

与这段摘录呼应的一篇文章:
http://blog.csdn.net/zzh87615/article/details/6083754
引用
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




遇到的一个bug:使用IdClass,居然不认@Column:
https://hibernate.onjira.com//browse/HHH-4256
1 楼 lg_asus 2011-11-11  
多谢~ 半天没解决好

将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为@Id(符合编程习惯)
:是将RegionalArticle  注解为@IdClass(RegionalArticlePK.class) ,然后把RegionalArticlePK中的那些属性注解为@Id吗?结果不行。

希望能解释一下~  多谢
2 楼 lg_asus 2011-11-14  
解决了, 原来主键类还要再把联合主键定义下~~~

感觉有点繁琐,但确实符合编程习惯~
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: