Hibernate version:
3.2.6
Name and version of the database you are using:
MySQL 5.0
Hi,
I have a simple data model that looks like this:
Container --> 1:N --> AbstractChild
AbstractChild uses the JOINED SUB CLASS strategy.
AbstractChild is the superclass of Child1,
Child1 is the superclass of Child2, and so on.
I looked at the DDL generated for this model, and I have a question: why doesn't hibernate declare ON DELETE CASCADE on the FK that it creates between Child1 and AbstractChild, between Child2 and Child1, and so on? Or - how do I make Hibernate add it?
Without ON DELETE CASCADE between a subclass and its superclass, I see 3 problems in this model:
1. if I bulk-delete a Container which has any AbstractChild-ren, hibernate generates the SQL: delete from container c where c.id=xxx, and it fails because the FK between any sub-Child and AbstractChild is not cascading the delete operation.
2. if I bulk-delete an AbstractChild, i.e. delete from AbstractChild c where c.id=xxx, hibernate generates N+1 SQL statements:
delete from abstractchild where id=xxx
delete from child1 where id=xxx
delete from child2 where id=xxx
...
delete from childN where id=xxx
instead of just generating a single statement:
delete from abstractchild where id=xxx
which would work great if ON DELETE CASCADE was declared on sub-Children.
3. if I bulk-delete a sub-child, e.g. delete from Child7 c where c.id=xxx, hibernate tries to first create a temporary table with the IDs of the objects that need to be removed, and unfortunately, at this point, it generates an invalid SQL statement, so I couldn't test further. But I assume that the SQL below should follow the ID selection:
delete from abstractchild where id in (temp table)
delete from child1 where id in (temp table)
delete from child2 where id in (temp table)
..
delete from child7 where id in (temp table)
...
delete from childN where id in (temp table)
note that it must go up to N because Class9 is also an instance of Class7, for example...
again: instead, it could simply generate a single statement:
delete from abstractchild where id=xxx
Can someone pls explain the reasons for this further?
thanks.
|