-->
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.  [ 2 posts ] 
Author Message
 Post subject: Bulk insert of association
PostPosted: Wed Aug 30, 2006 10:57 am 
Newbie

Joined: Wed Aug 23, 2006 10:11 am
Posts: 9
Is there any way to do a bulk insert on an association? I have classes like this (irrelevant details omitted) . . .

Code:
class Person
{
    // simple properties only
}

class Group
{
    Set<Person> getMembers();
}

class Event
{
    Set<Person> getAttendees();
}


. . .and a list of Groups. I'd like to add all the members from all the groups on my list to the set of attendees in an Event. However, the groups are quite large (on the order of a million members), so I can't just manipulate the lists in Java without running out of memory.

I tried making the Sets in question EXTRA lazy, and using the following to manipulate the lists:

Code:

counter = 0;
list = queryThatGetsAllRelevantMembers.getResultList();
for(Person p : list) {
   event.getAttendees().add(p);
   if(++counter % 1000 == 0) {
       em.flush();
       em.clear();
   }
}


based on some stuff I found on the web about bulk entity inserts, but it seems to have no effect (I run out of memory at the same point with or without the flush/clear code).

I'm about to write a native SQL statement to do the work, but that seems like such a hack. Is there a clean way to do this in Hibernate? Ideally, I'd like something like this:

Code:
insert into e.attendees select distinct p from Group g join g.members where g in (:groups) and e = :event


Top
 Profile  
 
 Post subject: Never mind - it was my cascade settings
PostPosted: Wed Aug 30, 2006 11:06 am 
Newbie

Joined: Wed Aug 23, 2006 10:11 am
Posts: 9
Never mind. The code shown works fine, once I changed the sets from CascadeType.ALL to CascadeType.REMOVE (ah, the curse of copy-and-paste).

Sorry if you wasted time on this.

Joe


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