java JPA/Hibernate:复合主键、@IdClass 或 @EmbeddedId 实现哪个更好,为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4024800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:26:42  来源:igfitidea点击:

JPA/Hibernate: What's better for composite primary keys, @IdClass or @EmbeddedId implementations and why?

javahibernatejpacomposite-primary-key

提问by Kawu

what's better for JPA/Hibernate composite primary keys, @IdClass or @EmbeddedId implementations and why?

JPA/Hibernate 复合主键、@IdClass 或 @EmbeddedId 实现哪个更好,为什么?

This is an intentionally naive question. I decided to use @EmbeddedId (for whatever reason) and I feel like I made the wrong choice. Dereferencing the embeddedId that contains the column properties is redundant and quite error-prone when coding.

这是一个故意幼稚的问题。我决定使用@EmbeddedId(无论出于何种原因),我觉得我做出了错误的选择。取消引用包含列属性的 EmbeddedId 是多余的,并且在编码时很容易出错。

Are there any more reasons for and/or against the other? Is the a recommendation by the JPA (spec)?

是否有更多的理由支持和/或反对另一个?是 JPA(规范)的推荐吗?

采纳答案by Kawu

As Pascal wrote here's part of the answer:

正如帕斯卡所写,这是答案的一部分:

Which annotation should I use: @IdClass or @EmbeddedId

我应该使用哪个注释:@IdClass 或 @EmbeddedId

In the end, I believe using @IdClassis much easier in practice, because you have to add the embeddedIdproperty name to dereference PK properties, while these aren't written for all non-PK properties.

最后,我相信@IdClass在实践中使用会容易得多,因为您必须添加embeddedId属性名称来取消引用 PK 属性,而这些并不是为所有非 PK 属性编写的。

You always have to remember exactly which properties are part of a PK and those which are not. That complicates writing JPQL queries unneccessarily.

您必须始终准确记住哪些属性是 PK 的一部分,哪些不是。这使编写 JPQL 查询变得不必要地复杂化。

Also, AFAIK the JPA 2.0 spec allows you to put @Idonto @XToX/@JoinColumn/s properties and it introduces the @MapsIdannotation, so that mapping identifying relationships (a.k.a. derived identifiers in JPA) are more natural to implement.

此外,AFAIK的JPA 2.0规范允许把@Id@XToX/ @JoinColumn/ s的性质和它介绍了@MapsId注释,以便标识映射关系(在JPA又名衍生标识符)是更自然实现。

回答by Victor Stafusa

First, if possible, avoid composite ids at all costs. But if you really need, I would recommend @EmbeddedId.

首先,如果可能,不惜一切代价避免使用复合 ID。但如果你真的需要,我会推荐@EmbeddedId.

@IdClassis basically a leftover from EJB 2.1 times to make it easier from migrating from BMP. In some other rare corner cases it may be better than @EmbeddedIdtoo. However, generally @EmbeddedIdis better and more OO since it encapsulates the concept os the key much better in the object.

@IdClass基本上是 EJB 2.1 时代的遗留物,以便更轻松地从 BMP 迁移。在其他一些罕见的角落情况下,它可能比@EmbeddedId太多更好。但是,通常@EmbeddedId更好,更面向对象,因为它在对象中更好地封装了 os the key 的概念。

You may want to use @AttributeOverride(s)if you need in the key field.

@AttributeOverride(s)如果您需要,您可能希望在关键字段中使用。

I don't see why do you think that dereferencing the embedded id is redundant and error-prone.

我不明白为什么你认为取消引用嵌入的 id 是多余的并且容易出错。

回答by drekka

I. Remember using idclass to do it. But I'd recommend doing everything you can to avoid multi field keys. They just create extra work.

I. 记得用 idclass 来做。但我建议尽一切可能避免多字段键。他们只是创造额外的工作。