如果我有这两个实体:
@Entity class A {
@OneToOne(cascade = CascadeType.MERGE, mappedBy = "a") B b;
//getters+setters
}
@Entity class B {
@OneToOne(cascade = CascadeType.MERGE) A a;
//getters+setters
}
JPA 规范是否保证这一点?
A a = new A();
B b = new B();
a.setB(b);
b.setA(a);
A managedA = entityManager.merge(a);
// after commit, managedA will have a reference to a managed B which JPA implementation will link to managedA instead of original unmanaged A
Assert.assertTrue(managedA.getB().getA() == managedA);
我的理解是它可以工作(至少在最新的 EclipseLink 中,至少有时是这样),但规范并不能保证这一点。
欢迎反例/规范摘录:-)