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

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

JPA cascade persistence with entity ElementCollection keys

javajpaeclipselink

提问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 fooContentfield forms a many-to-many relation between Barand Foo, so I thought it would be appropriate to use @ManyToManyto specify cascading for the field. However, when trying to persist a Barwith a couple of Foo → Stringvalues 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 Fooinstances. How should I annotate fooContentto get cascaded persists working?

显然,EclipseLink 不会级联我的Foo实例的持久性。我应该如何注释fooContent以使级联持续工作?

回答by Uooo

You don't need @ManyToManyannotation here. Operations on ElementCollections are always cascaded.

@ManyToMany这里不需要注释。对ElementCollections 的操作总是级联的。

回答by Mirko Klemm

It is an error to specify both @ElementCollectionand @ManyToManyat the same time. The two annotations denote different concepts of OR mapping a greater-than-one cardinality relationship.

它是同时指定一个错误@ElementCollection,并@ManyToMany在同一时间。这两个注释表示 OR 映射大于一基数关系的不同概念。

ElementCollectionis 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是一种严格的聚合或组合关系,其中集合中的元素严格归其父对象所有,与元素的任何交互,如查询等,都必须通过父对象完成。父元素与集合中元素的多样性始终是一对多的。在给定的时间点,元素实例只能与一个父元素相关。

ManyToManyrepresents 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. ManyToManyrelationships imply that the related instances can be related to any number of other instances by other declared relationships.

ManyToMany表示或多或少独立实体之间的关系,可以单独查询和操作,独立于声明用 注释的属性的实例@ManyToManyManyToMany关系意味着相关实例可以通过其他声明的关系与任意数量的其他实例相关。

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 实现都会为这样注释的属性显示错误或表现出“未定义”行为。