我有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