我有两个这样的 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();
// ...
}
如您所见,该字段在和fooContent
之间形成多对多关系,因此我认为使用该字段指定级联是合适的。但是,当尝试在地图中保留几个值时,我得到以下异常:Bar
Foo
@ManyToMany
Bar
Foo → String
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>>
显然,EclipseLink 不会级联我的Foo
实例的持久性。我应该如何注释fooContent
才能使级联持久化工作?