title: Force Eagerly load all dependencies in Hibernate
body: <p>Most of our Entities have Lazy loading as they are not required.
  In one specific scenario,function requires them to load eagerly, which is out of Transaction Scope.</p>
  
  <p>Here's the bit of implementation</p>
  
  <pre><code>@Transaction
  public E getFullyLoadedDetachedEntity(Long id) {
      E entity = dao.findById(id);
      Hibernate.initialize(entity);
      dao.detach(entity);
      return entity;
  }
  </code></pre>
  
  <p>But the problem is, <code>Entity(E) might have associated entities 'X' which might have 'Y' as lazy loaded.</code></p>
  
  <pre><code>class E{
     X x; //Lazy Load
  }
  class X{
     Y y; //Lazy load
  }
  </code></pre>
  
  <p>Now, <code>Hibernate.initialize(entity) may be loading only it's associated entities(X) but not the associated entites(Y) of X.</code></p>
  
  <p>How do I forcefully load all of the dependencies/Lazy loaded objects at any level.</p>
  
  <p><strong>Update</strong>
  I tried this one though</p>
  
  <pre><code>E entity = dao.findById(id);
  Hibernate.initialize(entity);
  for (X x : entity.getXList()) {
      Hibernate.initialize(x);
  }
  dao.detach(entity);
  </code></pre>
  
  <p>I am still getting LazyException on accessing the X.getY()</p>