开发者

Hibernate all-delete-orphan on a child class with two parent classes

开发者 https://www.devze.com 2023-01-07 06:40 出处:网络
I\'m looking into a problem, where there are two \"parent\" classes, P and Q that cascade all-delete-orphan to a \"child\" class, C. My intuition in Hibernate tells this is really a bad idea and I am

I'm looking into a problem, where there are two "parent" classes, P and Q that cascade all-delete-orphan to a "child" class, C. My intuition in Hibernate tells this is really a bad idea and I am getting an error message that probably confirms this when the code deletes an instance of P (i.e. session.delete(myP); ):

"del开发者_运维知识库eted object would be re-saved by cascade (remove deleted object from associations): [C#1]"

Can anyone confirm that having having two parent classes for a single child class is a bad idea when an instance of P and instance of Q can both act as the parent for the same instance of C?

Thanks!


My guess is that is only going to be a problem if you have both P and Q in the same transaction, simultaneously updating P and deleting from Q (or vice versa). Double parenthood should just add a layer of complexity to the transaction but it should still work the way you would expect.

session.beginTransaction();
P p = loadP(); 
p.remove(c);
session.commit(); //okay

session.beginTransaction();
P p = loadP();
Q q = loadQ();
p.remove(c);
q.alter(c);
session.commit(); //boom
0

精彩评论

暂无评论...
验证码 换一张
取 消