Java Embeddable 和 ElementCollection 嵌套
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22126397/
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
Embeddable and ElementCollection nesting
提问by Francesco
I have fairly typical scenario where there is a main @Entity and everything inside him is embeddable (so everything inside doesn't make sense without the parent). Now JPA 2.0 is blocking me to nest a @ElementCollection inside a @Embeddable defined in another @ElementCollection:
我有一个相当典型的场景,其中有一个主要的 @Entity 并且他里面的所有东西都是可嵌入的(所以如果没有父级,里面的所有东西都没有意义)。现在 JPA 2.0 阻止我将 @ElementCollection 嵌套在另一个 @ElementCollection 中定义的 @Embeddable 中:
JSR-317 2.6 Collections of Embeddable Classes and Basic Types An embeddable class (including an embeddable class within another embeddable class) that is contained within an element collection must not contain an element collection, nor may it contain a relationship to an entity other than a many-to-one or one-to-one relationship
JSR-317 2.6 可嵌入类和基本类型的集合包含在元素集合中的可嵌入类(包括另一个可嵌入类中的可嵌入类)不得包含元素集合,也不得包含与实体的关系,而不是多对一或一对一关系
Now the question is: why is this? A simple example:
现在的问题是:这是为什么?一个简单的例子:
@Entity
public class Tournament {
@Id
Long id;
@ElementCollection
@CollectionTable
private List<Edition>;
}
@Embeddable
public class Edition {
@ElementCollection
@CollectionTable
private List<Round>
}
@Embeddable
public class Round {
blabla;
}
What's the problem having this? This is just an example, you could define Round and Edition as Entity and solve the problem, but in my case for a number of reasons I need to enforce that something very nested doesn't make sense without his parent.
这有什么问题?这只是一个示例,您可以将 Round 和 Edition 定义为 Entity 并解决问题,但在我的情况下,出于多种原因,我需要强制执行某些非常嵌套的内容,如果没有他的父级就没有意义。
Why JPA 2.0 has to stop me doing this?
为什么 JPA 2.0 必须阻止我这样做?
采纳答案by stevestorey
Your situation violates the specification element you pasted in:
您的情况违反了您粘贴的规范元素:
Edition is itself @Embeddable, and contains an element collection of Round, and thus:
Edition 本身就是@Embeddable,并且包含一个 Round 元素集合,因此:
An embeddable class (Edition) that is contained within an element collection (Tournament.editions) must not contain an element collection (Edition.rounds).
包含在元素集合 (Tournament.editions) 中的可嵌入类 (Edition) 不得包含元素集合 (Edition.rounds)。
As to why you can't do this - if you look at the examples from http://en.wikibooks.org/wiki/Java_Persistence/ElementCollectionthen you'll see that the child (Edition) would be mapped with only a FK back to the owner (Tournament.id) without an ID column of its own - on the grounds that as a Weak entity, it has no ID of its own, and is only defined by reference to the ID of the Tournament.
至于为什么你不能这样做 - 如果你看一下http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection中的例子,那么你会看到孩子(版本)只会用 FK 映射返回给所有者(Tournament.id),没有自己的ID列——理由是作为一个Weak实体,它没有自己的ID,只能通过引用Tournament的ID来定义。
Taking the Round, if that too is a weak entity, then it should be defined by FK reference to the Edition - but we already said that has no ID of its own, so you can't map this in the DB without adding an ID to the Edition - at which point it would be an entity in its own right, and not be simply @Embeddable.
拿回合来说,如果那也是一个弱实体,那么它应该由对版本的 FK 引用定义 - 但我们已经说过它没有自己的 ID,所以你不能在不添加 ID 的情况下将它映射到数据库中到版本 - 在这一点上,它本身就是一个实体,而不仅仅是@Embeddable。
Looking at the Wikipedia example from the comment below - http://en.wikipedia.org/wiki/Weak_entity- the examples of weak entities there are OrderNumber, CustomerNumber etc - things which only ever make any sense when embedded in another object.
从下面的评论中查看维基百科示例 - http://en.wikipedia.org/wiki/Weak_entity- 弱实体的示例有 OrderNumber、CustomerNumber 等 - 只有在嵌入另一个对象时才有意义的东西。
You can still have entities which have parent mappings (i.e. a Tournament reference on the Edition) and/or bi-directional references. You can force the parent to be defined on the Edition with a nullable=false attribute on the @ManyToOne annotation, and thus enforce the requirements of your model.
您仍然可以拥有具有父映射(即版本上的锦标赛参考)和/或双向参考的实体。您可以使用 @ManyToOne 注释上的 nullable=false 属性强制在版本上定义父级,从而强制执行模型的要求。