gavin wrote:
I don't believe you.
See the unit test in org.hibernate.test.ondelete.OnDeleteTest.
You should, I've just discovered we are both right. The testsuite works as expected, but my application continued to have this strange behaviour.
The problem is related to heritance AND collections, and it's an half bug of Hibernate or, maybe, something you should avoid to do.
Have a look at this example.
Class A { ... }
Class B extends A { ... }
This heritance tree is mapped using joined-subclass and on-delete="cascade".
As in your testsuite, when I manually delete an object of class B, Hibernate issues a "delete from A": since the cascade is enable, the row in B is also deleted. Ok :-)
Let's add another class:
Class C {
...
private Set setOfObjectsOfClassB;
}
In this class, the set of objects of class B (not A !!!) is mapped using a <set> with on-delete="cascade" enabled.
When I delete an object of class C, Hibernate issues only a "delete from C" SQL statement. Since the cascade is enabled, the database also delete every row of B linked to that instance of class C, but NOT THE CORRESPONDING rows in A, since there's not a cascade mechanism ad database level from B to A. That's why I find pending rows in A.
If I DON'T specify on-cascade="delete" in the collection, Hibernate removes B objects manually before removing C, so it correctly issues "delete from A", then a "delete from C".
I think this is an half bug of Hibernate. I solved the problem not using the cascade on the collection, but there should be some cascade from B to A in the database. Unfortunately, I'm not sure this is possible.
Thanks for your help.