-->
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.  [ 4 posts ] 
Author Message
 Post subject: Efficient way to empty a collection?
PostPosted: Mon Apr 26, 2004 8:13 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Let's say I have a many-to-many from one entity A to another entity B. And let's say that there could be many, many, many B's associated with a given instance a1.

Now I want to delete a1. In order for this to work, I need to clean up the A-to-B association. Here's some code that could do it:

Code:
Iterator bInstances = a1.getBSet().iterator();
while (bInstances.hasNext()) {
    a1.getBSet().remove(bInstances.next());
}


But this doesn't work because this leads to ConcurrentModificationExceptions. So let's try this:

Code:
Set bSet = new HashSet(a1.getBSet());
Iterator bInstances = bSet.iterator();
while (bInstances.hasNext()) {
    a1.getBSet().remove(bInstances.next());
}


No more ConcurrentModificationExceptions because we're now iterating a different set than we're removing from. But now we have an efficiency problem! The bSet has to be completely populated, which could be a very large load, simply in order to turn around and delete it all. Not to mention that both of these alternatives involve one database round trip per A-to-B reference.

What I really want is a way to tell Hibernate to do the equivalent of "delete from a_to_b where a_to_b.a_id = <a1.getId()>" in one shot, so I can wipe the whole collection table in one database hit.

Is there any way to do this?

Cheers!
Rob


Top
 Profile  
 
 Post subject: Re: Efficient way to empty a collection?
PostPosted: Tue Apr 27, 2004 1:59 am 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
RobJellinghaus wrote:
No more ConcurrentModificationExceptions because we're now iterating a different set than we're removing from.


Why don't you just use iterator.remove() ? That way, you can remove from the same collection that you're currently iterating over.

Quote:
But now we have an efficiency problem! The bSet has to be completely populated, which could be a very large load, simply in order to turn around and delete it all. Not to mention that both of these alternatives involve one database round trip per A-to-B reference.

What I really want is a way to tell Hibernate to do the equivalent of "delete from a_to_b where a_to_b.a_id = <a1.getId()>" in one shot, so I can wipe the whole collection table in one database hit.

Is there any way to do this?


You could try if a1.getBSet().clear() helps.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 2:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Um.


a1.setBSet( new HashSet() );


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 3:11 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Okey dokey :-)

Sorry I didn't experiment more with this before posting.

Cheers!
Rob


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