One solution is to use the long life EntityManager pattern.
In this pattern, do not create and close an EntityManager for each transaction, instead create and use one EntityManager per thread (EntityManager's are not thread safe).
Then you can access more data in lazily loaded fields at any time (as long as the EntityManager for the object remains open).
The logic to manage one EntityManager per thread is available as utility classes in the Spring framework. Alternatively, write your own - I have done so - it's not that hard.
With regards to performance. I used to Kodo JDO, in that ORM implementation you could specify the number of rows to fetch at a time for lazily loaded fields. I don't know if the same feature is available in Hibernate - I've never looked.
The reason I've never looked is because if I have a large List then I write a query to page through it as required by the client, rather than accessing the whole List (with lazy loading) as part of some object graph.
If you have a large object graph, your approach, of loading the graph, and doing updates on it, then calling merge, will be very slow compared to the other more efficient approach of writing custom queries to fetch, update, and delete just the data you need.
Andy.
_________________ http:// www.techfoundry.com
|