I have a Hibernate data access layer sitting behind an in-memory object cache. For client side cache misses the in-memory cache will call out to the Hibernate layer to load the object graph into the cache. Due to how objects are serialized into the cache I need to load the entire object graph from Hibernate at one time (i.e. no Lazy loading). A scenario like this is covered in the Hibernate docs here. Here is the excerpt:
In an application with a separate business tier, the business logic must "prepare" all collections that the web tier needs before returning. This means that the business tier should load all the data and return all the data already initialized to the presentation/web tier that is required for a particular use case. Usually, the application calls Hibernate.initialize() for each collection that will be needed in the web tier (this call must occur before the session is closed) or retrieves the collection eagerly using a Hibernate query with a FETCH clause or a FetchMode.JOIN in Criteria. This is usually easier if you adopt the Command pattern instead of a Session Facade.
Since I cannot just enable Eager fetching on my entities directly (I end up with MultipleBagFetchException as documented in this StackOverflow question) I figured I would just keep the default Lazy loading behavior and then perform a Hibernate.initialize()
on each collection in the object so as to fully saturate the object. The problem is that my collections still end up empty.
To further confuse things this strategy seems to be working fine in my unit tests (my objects are saturated properly) but when I run this Hibernate layer as a plugin to my in-memory cache (I'm providing a jar containing my DAL that the in-memory cache will call out to for a cache miss) I'm seeing the behavior described.
Currently I'm handling my session with
<property name="current_session_context_class">thread</property>
and then calling
sessionFactory.getCurrentSession();
in my DAO, after which I do a get on my session for the object to load. As far as I can tell my session handling is the same for both my working unit tests and the non-working DAO. Any suggestions as to what is going on here. If you want more information let me know.