我的问题是我有一个对象 A,其中包含 B 对象的列表
@Entity
class A {
@OneToMany(cascade={CascadeType.MERGE})
List<B> list;
}
当我对对象 A 进行“合并”,然后在无状态 EJB 方法中调用“flush”时
em.merge(a); //a is of class A
em.flush(); //doesn't flush "list"
实际上是行不通的。未设置“list”的 B 对象的 id。
但是坚持和冲洗工作
em.persist(a);
em.flush(); // it works!
设置“list”的B对象的id。
我正在使用 EclipseLink。有人知道会发生什么吗?
最佳答案
it actually doesn't work because the ids of the B obects being part of the list "list" are not setted.
您应该避免使用 id 来实现 equals/hashCode
,当对象位于 List
中时,合约不应更改。无论如何,我无法使用 EclipseLink 2.0 重现您的问题:当我将 B
添加到时,A
上的合并会级联 B
上的插入列表。
关于java - 合并实体后的 Flush() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3121684/