Embeddable and ElementCollection nesting

放肆的年华 提交于 2019-12-04 08:21:02

Your situation violates the specification element you pasted in:

Edition is itself @Embeddable, and contains an element collection of Round, and thus:

An embeddable class (Edition) that is contained within an element collection (Tournament.editions) must not contain an element collection (Edition.rounds).

As to why you can't do this - if you look at the examples from http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection then 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.

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!