我有2個JPA實體之間有一個雙向關係。合併JPA實體返回舊值
@Entity
public class A {
@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE})
B b;
// ...
}
和
@Entity
public class B {
@OneToMany(mappedBy="b",cascade={CascadeType.PERSIST, CascadeType.MERGE})
Set<A> as = new HashSet<A>();
// ...
}
現在我更新分離A
其中也有一些Bs
反之亦然關係的一些字段值,並通過
public String save(A a) {
A returnedA = em.merge(a);
}
returnedA
現在已經把它合併A在更新之前的值。 我想,
FINEST: Merge clone with references [email protected]
FINEST: Register the existing object [email protected]
FINEST: Register the existing object [email protected]
FINEST: Register the existing object [email protected]
表明,在B中的引用As
(其中仍然有舊值)負責覆蓋新的?
有沒有人有提示如何防止這種情況發生?
任何想法,非常感謝!
在此先感謝。
一個快速的想法:從Bs中刪除合併級聯。 – Thomas
感謝您的建議,@Thomas。這肯定會有所幫助,但不幸的是,由於經常發生雙向更新,我寧願保留它。 – Dirk
不是一個理想的解決方案,但假設你擁有一個包含.save(A a)和.save(B B)的方法,你可以隨時刪除級聯和手動處理級聯.. – rhinds