title: Hibernate lazy-load application design
body: <p>I tend to use <a href="http://www.hibernate.org/docs">Hibernate</a> in combination with <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/orm.html#orm-hibernate">Spring</a> framework and it's declarative transaction demarcation capabilities (e.g., <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-declarative-annotations">@Transactional</a>). </p>
  
  <p>As we all known, hibernate tries to be as <em>non-invasive</em> and as <em>transparent</em> as possible, however this proves <strong>a bit more challenging</strong> when employing <strong><code>lazy-loaded</code></strong> relationships. </p>
  
  <hr>
  
  <p>I see a number of design alternatives with different levels of transparency. </p>
  
  <ol>
  <li>Make relationships not lazy-loaded (e.g., <code>fetchType=FetchType.EAGER)</code>
  <ul>
  <li>This vioalites the entire idea of lazy loading .. </li>
  </ul></li>
  <li>Initialize collections using <code>Hibernate.initialize(proxyObj);</code>
  <ul>
  <li>This implies relatively high-coupling to the DAO</li>
  <li>Although we can define an interface with <code>initialize</code>, other implementations are not guaranteed to provide any equivalent. </li>
  </ul></li>
  <li>Add transaction behaviour to the persistent <code>Model</code> objects themselves (using either <a href="http://download.oracle.com/javase/6/docs/api/">dynamic proxy</a> or <code>@Transactional</code>)
  <ul>
  <li>I've not tried the dynamic proxy approach, although I never seemed to get @Transactional working on the persistent objects themselves. Probably due to that hibernate is operation on a proxy to bein with. </li>
  <li>Loss of control when transactions are actually taking place</li>
  </ul></li>
  <li>Provide both lazy/non-lazy API, e.g, <code>loadData()</code> and <code>loadDataWithDeps()</code>
  <ul>
  <li>Forces the application to know when to employ which routine, again tight coupling</li>
  <li>Method overflow, <code>loadDataWithA()</code>, ...., <code>loadDataWithX()</code></li>
  </ul></li>
  <li>Force lookup for dependencies, e.g., by only providing <code>byId()</code> operations
  <ul>
  <li>Requires alot of non-object oriented routines, e.g., <code>findZzzById(zid)</code>, and then <code>getYyyIds(zid)</code> instead of <code>z.getY()</code></li>
  <li>It can be useful to fetch each object in a collection one-by-one if there's a large processing overhead between the transactions. </li>
  </ul></li>
  <li>Make part of the <em>application</em> @Transactional instead of only the <a href="http://en.wikipedia.org/wiki/Data_access_object">DAO</a>
  <ul>
  <li>Possible considerations of nested transactions</li>
  <li>Requires routines adapted for transaction management (e.g., suffiently small)</li>
  <li>Small programmatic impact, although might result in large transactions</li>
  </ul></li>
  <li>Provide the DAO with dynamic <a href="http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e3524">fetch profiles</a>, e.g., <code>loadData(id, fetchProfile);</code>
  <ul>
  <li>Applications must know which profile to use when</li>
  </ul></li>
  <li>AoP type of transactions, e.g., intercept operations and perform transactions when necessary
  <ul>
  <li>Requires byte-code manipulation or proxy usage</li>
  <li>Loss of control when transactions are performed</li>
  <li>Black magic, as always :)</li>
  </ul></li>
  </ol>
  
  <p>Did I miss any option?</p>
  
  <hr>
  
  <p>Which is your preferred approach when trying to minimize the impact of <code>lazy-loaded</code> relationships in your application design? </p>
  
  <p>(Oh, and sorry for <a href="http://www.urbandictionary.com/define.php?term=wall%20of%20text">WoT</a>)</p>