我有兩個JPA實體類似這樣的JPA級聯持久性:與實體ElementCollection鍵
@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
以使級聯持續運行?
你的'Foo'是怎麼樣的?異常看起來像'Foo'和另一個實體之間存在問題。 – Uooo
@Uooo錯誤仍然發生在上面的確切代碼(即Foo內沒有更多字段) – dflemstr
我遇到了同樣的問題。你有沒有找到任何解決方案? – drame