How to disable or freeze Hibernate lazy loading after an object graph has already been created in a session

Viewed 51

In Hibernate (actually I am running a 10 year old version, which I had already modified and is working wonderfully for my purpose), I have the following use case:

In a session, I am loading and editing an object graph in memory, it involves lazy collections and all. The entire object graph of interest has been walked, and while lots more lazy collections may be accessed to pull more objects into the graph, we don't want that, as the whole database being connected it could suck the entire 100 GB database into memory.

Now that graph is saved, flushed, and possibly committed to the database. The session may still be open. Whether or not the session is still open should not matter for what comes next.

Next I want to traverse that whole object graph as it is now in memory, whatever of the lazy stuff has been fetched, I traverse that. But whatever has not been fetched, I do not now want to fetch it, because I do not want to suck in the entire database through connections I didn't care about in my transaction. I just want the object graph as it is now, nothing new.

How can I disable all further lazy loading / fetching in a Session but without getting Exceptions when I am trying to get elements from lazy collections, etc.?

0 Answers
Related