title: Hibernate.initialize() doesn't seem to work on lazy loaded Collections
body: <p>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 <a href="http://docs.jboss.org/hibernate/core/3.5/reference/en/html/performance.html#performance-fetching-initialization" rel="nofollow noreferrer" title="here">here</a>. Here is the excerpt:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>Since I cannot just enable Eager fetching on my entities directly (I end up with MultipleBagFetchException as documented in <a href="https://stackoverflow.com/questions/4334970/hibernate-cannot-simultaneously-fetch-multiple-bags">this</a> StackOverflow question) I figured I would just keep the default Lazy loading behavior and then perform a <code>Hibernate.initialize()</code> on each collection in the object so as to fully saturate the object. The problem is that my collections still end up empty.</p>
  
  <p>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.</p>
  
  <p>Currently I'm handling my session with </p>
  
  <pre><code>    &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  </code></pre>
  
  <p>and then calling</p>
  
  <pre><code>    sessionFactory.getCurrentSession();
  </code></pre>
  
  <p>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.</p>