-->
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.  [ 3 posts ] 
Author Message
 Post subject: One shot delete and one to many collections
PostPosted: Fri Sep 19, 2008 6:39 am 
Newbie

Joined: Wed Dec 19, 2007 7:10 am
Posts: 10
Hello,

I'm using Hiberate version 3.3.1 and trying to write a very simple one to many mapping where deletes are performed efficiently. I've got a number of mappings and am happy with Hibernate for inserting/selecting/etc., but I'm looking at how to efficiently delete relationship, and failing to find a good example.

I have two objects, Application and Event. Application has a set of Events (so a one to many relationship).

Here are the relevant mappings:

<hibernate-mapping>
<class name="Application">
<cache usage="read-write"/>
<id name="id" column="applicationId">
<generator class="native"/>
</id>

<set name="events" inverse="true"
cascade="all,delete-orphan" lazy="extra">
<key column="applicationId" />
<one-to-many class="Event" />
</set>
</class>
</hibernate>

<hibernate-mapping>
<class name="Event" polymorphism="implicit">
<id name="id" column="eventId">
<generator class="native"/>
</id>
<many-to-one name="application"
column="applicationId"
not-null="true"/>
</class>
</hibernate>

This relationship works perfectly correct for adding/updating - I can add events to the set in Application, call session.persist(application) and everything is updated as I would expect. However the delete is very inefficient. If I delete an application then hibernate deletes each event separately;

Hiberate: delete from Event where eventId=?
etc.

What I'd like it to do is:

delete from Event where applicationId=?

This seems a fairly basic requirement so I apologise for clearly missing the documentation, but could someone tell me how it's done?

Thanks,


J


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 19, 2008 6:44 am 
Newbie

Joined: Wed Dec 19, 2007 7:10 am
Posts: 10
I see the solution:

<set name="events" inverse="true"
cascade="all,delete-orphan" lazy="extra">
<key column="applicationId" />
<one-to-many class="Event" />
</set>

should be:

<set name="events" inverse="true"
cascade="all,delete-orphan" lazy="extra">
<key column="applicationId" on-delete="cascade" />
<one-to-many class="Event" />
</set>

I would have assumed a cascade delete would be the default behaviour!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 19, 2008 10:52 am 
Newbie

Joined: Sun Sep 07, 2008 11:07 pm
Posts: 17
I am also running into the same problem where multiple deletes are getting executed

As suggested by you If we put this attribute on-delete = 'cascade' that means we are setting the cascade at the Database level and Hibernate is not executing the delete statements.

Is there any way to tell Hibernate to delete the children at one shot?

delete children where parent_id = ?


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