1
我有2個父類A,C和2個子代:B,D。兩種關係都啓用了傳遞式持久性。然而堅持D需要B持久。冬眠中的傳遞持久性命令
class A {
@OneToMany(cascade = {CascadeType.PERSIST}, mappedBy = "a")
public Set<B> getBs() {
...
}
}
class B {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "a_id", nullable = false)
public A getA() {
...
}
}
class C {
@OneToMany(cascade = {CascadeType.PERSIST}, mappedBy = "c")
public Set<D> getDs() {
...
}
}
class D {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "b_id", nullable = false)
public B getB() {
...
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "c_id", nullable = false)
public C getC() {
...
}
}
什麼我想是B和d保存在一個鏡頭:
A a = entityManager.getReference(...);
C c = entityManager.getReference(...);
B b = new B();
D d = new D();
d.setB(b);
a.add(b);
b.setA(a);
c.add(d);
d.setC(c);
entityManager.flush();
我想這應該是可能的。但是,我得到一個TransientObjectException,告訴B在試圖保存D時需要持久化。
是否有特定的級聯關係由hibernate處理?