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