-->
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.  [ 5 posts ] 
Author Message
 Post subject: hbm2ddl and cascades
PostPosted: Wed Nov 24, 2004 3:45 pm 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
I've been trying to resolve a problem using MySQL where an expected cascade delete from parent to children has not been happening. XDoclet is used to generate the mapping, from which this is a relevant excerpt (from the mapping for the parent, AdminLevel):

<set name="adminUsers"
lazy="false"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted">
<key column="admin_level_id"></key>
<one-to-many class="sailingnetworks.om.AdminUser" />
</set>

hbm2ddl is used to generate the schema. Now the odd, and surely significant, thing is that the schema generated has no mention of cascades. The relevant line is this:

alter table admin_user add index FK29045ABB968E6EA6 (admin_level_id), add constraint FK29045ABB968E6EA6 foreign key (admin_level_id) references admin_level (id);

When run, we get a foreign key constraint error instead of a cascade. However, manually adding 'on cascade delete' to the end of the above 'alter table...' line makes it all work as planned. Does hbm2ddl not correctly generate the 'on cascade xxx' code?

Hibernate 2.1.6, MySQL 4.0.22 on Mandrake Linux 10.1.


Top
 Profile  
 
 Post subject: Re: hbm2ddl and cascades
PostPosted: Fri Nov 26, 2004 6:46 am 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
J2R wrote:
Hibernate 2.1.6, MySQL 4.0.22 on Mandrake Linux 10.1.


Correction: Hibernate 2.1.4


Top
 Profile  
 
 Post subject: Re: hbm2ddl and cascades
PostPosted: Fri Nov 26, 2004 8:52 am 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
J2R wrote:
J2R wrote:
Hibernate 2.1.6, MySQL 4.0.22 on Mandrake Linux 10.1.


Correction: Hibernate 2.1.4


Further correction: an upgrade to 2.1.7c makes no difference.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 12:07 pm 
Beginner
Beginner

Joined: Mon Sep 05, 2005 4:48 pm
Posts: 31
This is STILL not working in 3.2.x . . . I have found numerous posts on the web pertaining to this problem, dating back to 2004, but it appears the issue has still not been addressed.

Here are some of the issues logged:

http://opensource.atlassian.com/projects/hibernate/browse/HHH-2263
http://opensource.atlassian.com/projects/hibernate/browse/ANN-598
http://opensource.atlassian.com/projects/hibernate/browse/HB-1094

The interesting thing here . . . notice on HB-1094, Gavin King mentions that this has been added to the Hibernate 3 branch. If you read Section 5.1.19 of the Hibernate Docs, it states . . .

Quote:
We recommend that for systems where delete performance is important, all keys should be defined on-delete="cascade", and Hibernate will use a database-level ON CASCADE DELETE constraint, instead of many individual DELETE statements. Be aware that this feature bypasses Hibernate's usual optimistic locking strategy for versioned data


This would imply that by simply placing on-delete="cascade" to a key on, say, a one-to-many set, this should add ON DELETE CASCADE to the DDL generated by way of HBM2DDL . . . but it doesn't!!!

Here is a one-to-many I have set up, where I want ON CASCADE DELETE to apply to the foreign key, but it is not being added to the DDL:

Code:
<class name="com.xrite.ind.core.Group" table="tblGroup" >
        <id name="Id" type="long">
            <column name="groupID" />
            <generator class="native" />
        </id>
    <set name="userGroups" lazy="true" table="tblUserGroup_DataGroup" inverse="true" >
            <key column="groupID" on-delete="cascade"/>
            <many-to-many column="userGroupID" class="com.xrite.ind.backcheck.users.UserGroup"/>
        </set>


if I take the on-delete="cascade" off and add cascading on the set, I get individual delete statements, which also results in ALL of the objects being loaded(their children and their children, and so on) and subsequently deleted(which takes forever and requires a lot of memory).

Is there a way to get ON CASCADE DELETE added to HBM2DDL generated DDL or no?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 19, 2007 4:05 pm 
Beginner
Beginner

Joined: Mon Sep 05, 2005 4:48 pm
Posts: 31
It appears what fixed my problem is the hibernate.dialect property. This needs to be set at a value of org.hibernate.dialect.MySQLInnoDBDialect rather than org.hibernate.dialect.MySQLDialect

This works for one-to-many deletes, but not for many-to-many. There still does not appear to be a good way to perform a many-to-many delete, which cascades from the left side table, through to the association table, removing the association table's rows, then removing the left side table's rows in this case. The only way I have seen where this is possible is removing the object from the parent's collection in Java code, which I don't want to do. I would hope there is a way to simply call session.delete(parent) and have the cascading propagate down to all levels.


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

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.