-->
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.  [ 6 posts ] 
Author Message
 Post subject: N-N and bulk delete
PostPosted: Thu Aug 25, 2005 7:34 am 
Newbie

Joined: Fri Mar 18, 2005 5:35 am
Posts: 8
Hi, i've used Hibernate 3.0.5.

I have an Entity named Group that have n-n relation with itself (an group should be composed by other groups); the mapping is:

<set name="composedGroup" table="cs_mergesGroup" lazy="true" inverse="false" cascade="save-update">
<key column="fk_groupId" />
<many-to-many class="core.dto.tests.Group" column="fk_originalGroupId" />
</set>
<set name="groupMembership" table="cs_mergesGroup" lazy="true" inverse="true" cascade="none">
<key column="fk_originalGroupId" />
<many-to-many class="core.dto.tests.Group" column="fk_groupId"/>
</set>

Example of populing:
Group g1 = (Group) session.load(Group.class,1);
Group g2 = (Group) session.load(Group.class,2);
Group g3 = (Group) session.load(Group.class,3);

g3.getComposedGroup().add(g1);
g1.getGroupMembership().add(g3);

g3.getComposedGroup().add(g2);
g2.getGroupMembership().add(g3);

session.update(g3);

Now i have a group g3 that is formed by group g2 and g1....

If i perform this code all works correctly:
Group group = (Group)session.load(Group.class,3);
session.delete(group);

Now group g3 are deleted and it's relations (but g2 and g1 still live)

If i use bulk delete:
Query query = session.createQuery("delete Group where groupId = 3");
query.executeUpdate();

i obtain an error caused by db violation:
"Cannot delete or update a parent row: a foreign key constraint fails"

This is an limitation on bulk delete? It not manage relation?
Thanks in advance
Alessio


Top
 Profile  
 
 Post subject: Bulk Delete and Cascades
PostPosted: Fri Mar 10, 2006 3:04 pm 
Newbie

Joined: Tue Jan 10, 2006 1:30 pm
Posts: 14
I have received a similar error.

I have a Parent Class that has n number of Children associated with it. Cascade is set to all-delete-orphan.

When I do a bulk delete I get a constraint violation (if there was a db constrant on that column) or (if there is no constraint on that column) I get a bunch of orphaned children classes:

this.sess.createQuery("delete parent where id in (1,2,3)").executeQuery();

produces this SQL:

delete from PARENT_TABLE where PARENT_ID in (1,2,3)

The Children never get deleted.

Does anyone know if bulk deletes are supposed to cascade deletes?

_________________
Brian


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 3:28 pm 
Newbie

Joined: Tue Jan 10, 2006 1:30 pm
Posts: 14
I think I just found the answer to my own question...

This is some good information on Hibernate Bulk Functionality:

http://blog.hibernate.org/cgi-bin/blosx ... basic.html

http://blog.hibernate.org/cgi-bin/blosx ... ulti-table

In this article I see:


"From the EJB3 persistence specification:
Bulk update and delete operations apply to entities of a single entity class (together with its subclasses, if any). Only one entity abstract schema type may be specified in the FROM or UPDATE clause.
A delete operation only applies to entities of the specified class and its subclasses. It does not cascade to related entities.
...
One very interesting thing to point out there. The specification specifically disallows cascading of the delete to releated entities (not including, abviously, db-level cascades). "

So... bulk deletes won't do cascades for us.

_________________
Brian


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 11:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Hibernate will add an HQL construct for specifying this. Because of the EJB3 spec, we cannot *automatically* cascade to associations. But HQL will allow specifying a delete should cascade to a particular association, much like the current fetch keyword. Something like "delete from A as a cascade a.b"...

There is a JIRA case for this enhancement if you want to track it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 9:42 pm 
Newbie

Joined: Sun Jul 31, 2005 7:53 pm
Posts: 17
Steve,

Apologies for being dumb: can you provide a link to this JIRA and/or is it now completed?

I am hoping there is some magic HQL-specific 'do cascading deletes' keyword, but I can't find it in the documentation? Was this dropped?

Thanks,

Richard.


Top
 Profile  
 
 Post subject: Jira Issue regarding this
PostPosted: Wed May 21, 2008 3:37 am 
Newbie

Joined: Fri Feb 22, 2008 12:45 pm
Posts: 5
There is a JIRA Issue opened since July 2006 regarding this problem


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.