-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: ON DELETE CASCADE between subclass and superclass?
PostPosted: Wed Apr 30, 2008 3:33 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.