I have a bidirectional many-to-many mapping of sets (foo and bar). Each class has it's own table and a mapping table in between (foo_bar_mapping).
I'm doing some log loading and transforming those logs into the normalized database form, the issue is the speed at which these updates happen...
the code for adding looks like...
in the Foo class... note: getBars() returns a Set of bars.
Code:
public void addBar(Bar aBar) {
if (!getBars().contains(aBar) {
getBars().add(aBar);
aBar.addFoo(this);
}
}
in the Bar class... note: getFoos() returns a Set of foos.
Code:
public void addFoo(Foo aFoo) {
if (!getFoos().contains(aFoo) {
getFoos().add(aFoo);
aFoo.addBar(this);
}
}
I can call aFoo.addBar(aBar), save the objects via my DAOs and it works great, but as soon as their gets to be a lot of bars for aFoo it starts to get really slow. The problem is that Hibernate will populate the Set of all bars in aFoo.bars from the database to check for duplicate items. In some casses this can be half a million or more bars for me.
My question is if there is a better techique in hibernate for doing this insert
I could obviously create an SQL statement to insert directly to the foo_bar_mapping table, but it would be the only place in my system that has an SQL statement and that just makes me sad.
I would prefer not to make it a uni-directional mapping as there is places in my code where i use both aFoo.getBars and aBar.getFoos().
I've tried to cache as much as possible, but just the very act of populating that list is killing me on insert times. I haven't found any other way to insert to a mapping table through Hibernate that is referenced only via the foo and bar mapping documents.
Any thoughts, suggestions, techiques on how to do fast inserts for this type of mapping would be most appreciated.