views:

96

answers:

1

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); ):

"deleted 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!

+1  A: 

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
Anthony Bishopric
Thanks for keeping the dream alive.
Andy Hull