When you say you "use [the SearchItems] again", is that after another web page submit? Are you sure you're in the same thread? If not then the lazy references will be broken. If you put the Search and SearchItem objects into a web context, then retrieve them from it after the next submission, their links won't be guaranteed to survive, especially if they've been proxied and haven't yet been initialized by CGLib.
If you app is going to scale at all, then lots of openSession calls and no closeSessions is going to cause you problems, even with flush=always. Also, it'll be very fragile: if there's any strangeness to your data, something like a delete attempt foiled by referential integrity, then your session will be essentially unusable. If you never close it, anything could happen. Expect problems.
It is probable that what you want is a large 2nd-level cache (ehcache or similar) shared between multiple sessions. Check the docs for the correct solution. They are quite explicit on this: Hibernate sessions are not intended to stay around beyond a single web page action, or action similar duration. All you need to do is keep a single SessionFactory around, which maintains a reference to a common cache. Then each Session opened by the factory will be able to share the context of the cache.
|