Java JPA 级联持久性与实体 ElementCollection 键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19428351/
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
JPA cascade persistence with entity ElementCollection keys
提问by dflemstr
I have two JPA entities like this:
我有两个这样的 JPA 实体:
@Entity
class Foo {
@Id
private long id;
// ...
}
@Entity
class Bar {
@ElementCollection(targetClass = String.class, fetch = FetchType.LAZY)
@MapKeyJoinColumn(name = "foo_id", referencedColumnName = "id")
@MapKeyClass(Foo.class)
@Column(name = "content")
@CollectionTable(name = "bar_foo_content",
joinColumns = @JoinColumn(name = "bar_id", referencedColumnName = "id"))
@ManyToMany(cascade = CascadeType.ALL)
private Map<Foo, String> fooContent = Maps.newHashMap();
// ...
}
As you can see, the fooContent
field forms a many-to-many relation between Bar
and Foo
, so I thought it would be appropriate to use @ManyToMany
to specify cascading for the field. However, when trying to persist a Bar
with a couple of Foo → String
values in the map, I get the following exception:
如您所见,该fooContent
字段在Bar
和之间形成多对多关系Foo
,因此我认为使用@ManyToMany
指定该字段的级联是合适的。但是,当尝试在地图中保留Bar
带有几个Foo → String
值的a 时,出现以下异常:
javax.persistence.RollbackException: java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: <<instance of Foo>>
Clearly, EclipseLink does not cascade the persistence of my Foo
instances. How should I annotate fooContent
to get cascaded persists working?
显然,EclipseLink 不会级联我的Foo
实例的持久性。我应该如何注释fooContent
以使级联持续工作?
回答by Uooo
You don't need @ManyToMany
annotation here. Operations on ElementCollection
s are always cascaded.
@ManyToMany
这里不需要注释。对ElementCollection
s 的操作总是级联的。
回答by Mirko Klemm
It is an error to specify both @ElementCollection
and @ManyToMany
at the same time. The two annotations denote different concepts of OR mapping a greater-than-one cardinality relationship.
它是同时指定一个错误@ElementCollection
,并@ManyToMany
在同一时间。这两个注释表示 OR 映射大于一基数关系的不同概念。
ElementCollection
is a strict aggregation or composition relationship, where elements in the collection are strictly owned by their parent object, and any interaction with the elements, like querying etc. have to be done via the parent. The multiplicity of the parent versus the elements in the collection is always one to many. The element instances can be related to only one parent at a given point in time.
ElementCollection
是一种严格的聚合或组合关系,其中集合中的元素严格归其父对象所有,与元素的任何交互,如查询等,都必须通过父对象完成。父元素与集合中元素的多样性始终是一对多的。在给定的时间点,元素实例只能与一个父元素相关。
ManyToMany
represents a relationship between more or less independent entities, that can be queried and manipulated individually and independently of the instance declaring the property annotated with @ManyToMany
. ManyToMany
relationships imply that the related instances can be related to any number of other instances by other declared relationships.
ManyToMany
表示或多或少独立实体之间的关系,可以单独查询和操作,独立于声明用 注释的属性的实例@ManyToMany
。ManyToMany
关系意味着相关实例可以通过其他声明的关系与任意数量的其他实例相关。
I'd expect that any standards-compliant JPA implementation will either show an error or exhibit "undefined" behavior for a property annotated like this.
我希望任何符合标准的 JPA 实现都会为这样注释的属性显示错误或表现出“未定义”行为。