Hi,
I've been working on a project involving Hibernate for almost a year now, and the more complex the project gets, the more issues arise. So I wanted to ask for some advice before the situation becomes unbearable.
The main problem I'm facing is a collision between using criteria/filters and doing updates/inserts on detached objects. It looks like when performing criteria searches on the same object (same pk) twice within single session (without clearing the session), just with different filter parameters, cached results from the first search are returned again when the second search is performed. I tried to avoid that by using HQL to find the matching objects, and then calling session.load(pk) to get an object containing those matches, but that approach does not seem to work when lazy loading is enabled (the returned object always contains full object state, regardless of the subset returned by HQL query).
So I changed the code to clear the session, so all search methods would always start with clean slate. That fixed the search problems, but it broke the updates/inserts.
I have many situations where I use a search method to locate an object (which clears session), then I call another search method, and another, and each of them clears the session. Then I try to set the data from all those calls on the first object, and update it.
So in that scenario I started to get exceptions like non unique object and duplicate collections found, and my code stopped working again.
So I'd like to find out if anyone has had to work with similar scenario, and how did you solve those problems? I tried using session.merge instead of session.saveOrUpdate, but then I started getting TransientObjectExceptions and such.
Is there a way out of this dilemma? Is it better to clear the session, and deal with duplicate objects and such, or is it better to maintain the session and find a solution for search criteria issues?
Thanks,
Bratek
|