I am using Hibernate 3.0. I have a one-to-many relationship, let's called it Parent and Child.
The problem that I'm having is that commits are horribly slow ( >2 minutes, which is timing out the database connection ). I did some debugging to try to figure out why they are so slow, and here is what I found:
* When a Child record is saved, Hibernate calls getForInsert() on a BackrefGetter, which seems to have something to do with the one-to-many relationship.
* getForInsert() then calls getOwnerId() on the PersistenceContext.
* getOwnerId() tries to figure out which Parent object owns the Child object being saved. From the best that I can tell from looking at the source code, it iterates through *every* object in the session who's type is Parent, and looks at the children relationship to see if that particular Parent object is the one that owns the child being saved.
* I have a lot of objects in my session, and many of my Parent objects in the session have not had their children() collection loaded yet, so this triggers a bunch of SQL queries to the db to see whether they are the owner of the child record being saved. This is what leads to the performance problems.
Why does Hibernate do this, and how can I solve this problem? I cannot easily upgrade to a newer version of Hibernate, as I have applied my own bug fixes to the version 3.0 source code.
|