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: cascade delete not very efficient..
PostPosted: Thu Nov 11, 2004 7:05 am 
Beginner
Beginner

Joined: Wed Sep 24, 2003 11:06 am
Posts: 21
Location: Denmark
Hibernate version:
2.1.6
Mapping documents:
Parent:
<bag
name="hits"
lazy="false"
inverse="true"
cascade="all"
>
<key
column="Identification_ID"
>
</key>

<one-to-many
class="com.mds.pepland.persistence.IdentificationHit"
/>
</bag>

Child:
<many-to-one
name="identification"
class="com.mds.pepland.persistence.IdentificationImpl"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="Identification_ID"
not-null="true"
/>

Name and version of the database you are using:
MySQL 4

I have a chain of parent-child relationships several levels deep.. they are all mapped as shown above..

when i delete for instance an Identification object, Hibernate selects everything in the chain below the object I'm deleting and then deletes each record one by one using "delete from xx where primary_key=yy"..
Is there a way to get Hibernate to use the foreign keys in the delete statements instead? (for obvious performance reasons)..

as a side note I can say that none of the objects that must be deleted has a second level cache...

_________________
Hibernate rocks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 7:07 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
parent.setChildrenCollection(null);
tx.commit();

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 7:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Naw, that doesn't help for associations.

Efficient cascade delete is in HB3.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 7:23 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Right, for future readers: this trick only works for collections of value typed objects.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 12:21 am 
Newbie

Joined: Sun Dec 17, 2006 10:08 am
Posts: 2
gavin wrote:
Naw, that doesn't help for associations.

Efficient cascade delete is in HB3.


Do you mean oneshotdelete?
http://www.hibernate.org/hib_docs/refer ... shotdelete

I have tried,but result is a single update statement:
java code:
invoice.getInvoiceDetails().clear();
session.delete(invoice);
hibernate trace:
Hibernate: update SCOTT.INVOICE_DETAIL set INVOICE_ID=null where INVOICE_ID=?
Hibernate: delete from SCOTT.INVOICE where ID=?

I want "delete SCOTT.INVOICE_DETAIL where INVOICE_ID=?"

invoice.hbm.xml
<set name="invoiceDetails" inverse="false" cascade="delete">
<key column="INVOICE_ID"/>
<one-to-many class="entity.InvoiceDetail"/>
</set>

invoicedetail.hbm.xml
<many-to-one name="invoice" class="entity.Invoice">
<column name="INVOICE_ID" length="9" precision="0" not-null="false"/>
</many-to-one>

any idea?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 05, 2007 9:13 am 
Newbie

Joined: Sun Dec 17, 2006 10:08 am
Posts: 2
I found hibernate can do a smart cascade delete with idbag

<idbag name="invoiceDetails" table="INVOICE_DETAIL" cascade="delete" order-by="ID asc">
<collection-id column="ID" type="int" >
<generator class="native"/>
</collection-id>
<key column="INVOICE_ID"/>
<composite-element class="entity.InvoiceDetail">
<parent name="invoice"/>
...
</property>
</composite-element>
</idbag>

Hibernate: delete from INVOICE_DETAIL where INVOICE_ID=?
Hibernate: delete from SCOTT.INVOICE where ID=?


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.