title: Replacing a full ORM (JPA/Hibernate) by a lighter solution : Recommended patterns for load/save?
body: <p>I'm developing a new Java web application and I'm exploring new ways (new for me!) to persist the data. I mostly have experience with JPA &amp; Hibernate but, except for simple cases, I think this kind of full ORM can become quite complex. Plus, I don't like working with them that much. I'm looking for a new solution, probably closer to SQL.</p>
  
  <p>The solutions I'm currently investigating :</p>
  
  <ul>
  <li><a href="http://mybatis.github.io/mybatis-3/" rel="noreferrer">MyBatis</a></li>
  <li><a href="http://www.jooq.org/" rel="noreferrer">JOOQ</a></li>
  <li>Plain SQL/JDBC, potentially with <a href="http://commons.apache.org/proper/commons-dbutils/" rel="noreferrer">DbUtils</a> or some other basic utility libraries.</li>
  </ul>
  
  <p>But there are two use cases I'm worrying about with those solutions, compared to Hibernate. I'd like to know what are the recommended patterns for those use cases.</p>
  
  <hr>
  
  <h3>Use Case 1 - Fetching an entity and accessing some of its associated children and grandchildren entities.</h3>
  
  <ul>
  <li>Let's say I have a <code>Person</code> entity.
  
  <ul>
  <li>This <code>Person</code> has an associated <code>Address</code> entity.
  
  <ul>
  <li>This <code>Address</code> has an associated <code>City</code> entity.
  
  <ul>
  <li>This <code>City</code> entity has a <code>name</code> property.</li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>The full path to access the name of the city, starting from the person entity, would be : </p>
  
  <pre><code>person.address.city.name
  </code></pre>
  
  <p>Now, let's say I load the Person entity from a <code>PersonService</code>, with this method :</p>
  
  <pre><code>public Person findPersonById(long id)
  {
      // ...
  }
  </code></pre>
  
  <p>Using Hibernate, the entities associated to the <code>Person</code> could be lazily loaded, on demand, so it would be possible to access <code>person.address.city.name</code> and be sure I have access to this property (as long as all the entities in that chain are not nullable).</p>
  
  <p>But using anyone of the 3 solutions I'm investigating, it's more complicated. With those solutions, what are the recommended patterns to take care of this use case? Upfront, I see 3 possible patterns:</p>
  
  <ol>
  <li><p>All the required associated children and grandchildren entities could be eagerly loaded by the SQL query used. </p>
  
  <p>But the issue I see with this solution is that there may be some other code that needs to access <em>other</em> entities/properties paths from the <code>Person</code> entity. For example, maybe some code will need access to <code>person.job.salary.currency</code>. If I want to reuse the <code>findPersonById()</code> method I already have, the SQL query will then need to load more information! Not only the associated <code>address-&gt;city</code> entity but also the associated <code>job-&gt;salary</code> entity. </p>
  
  <p>Now what if there are <strong>10</strong> other places that need to access other information starting from the person entity? Should I always eagerly load <em>all</em> the potentially required information? Or maybe have 12 different service methods to load a person entity? :</p>
  
  <pre><code>findPersonById_simple(long id)
  
  findPersonById_withAdressCity(long id)
  
  findPersonById_withJob(long id)
  
  findPersonById_withAdressCityAndJob(long id)
  
  ...
  </code></pre>
  
  <p>But then everytime I would use a <code>Person</code> entity, I would have to know what has been loaded with it and what hasn't... It could be quite cumbersome, right?</p></li>
  <li><p>In the <code>getAddress()</code> getter method of the <code>Person</code> entity, could there be a check to see if the address has already been loaded and, if not, lazily load it? It this a frequently used pattern in real life applications?</p></li>
  <li><p>Are there other patterns that can be used to make sure I can access the entities/properties I need from a loaded Model?</p></li>
  </ol>
  
  <hr>
  
  <h3>Use Case 2 - Saving an entity and making sure its associated and modified entities are also saved.</h3>
  
  <p>I want to be able to save a <code>Person</code> entity using this <code>PersonService</code>'s method :</p>
  
  <pre><code>public void savePerson(Person person)
  {
      // ...
  }
  </code></pre>
  
  <p>If I have a <code>Person</code> entity and I change <code>person.address.city.name</code> to something else, how can I make sure the <code>City</code> entity modifications will be persisted when I save the <code>Person</code>? Using Hibernate, it can be easy to <em>cascade</em> the save operation to the associated entities. What about the solutions I'm investigating?</p>
  
  <ol>
  <li><p>Should I use some kind of <em>dirty</em> flag to know what associated entities also have to be saved when I save the person?</p></li>
  <li><p>Are there any other known patterns useful to deal with this use case?</p></li>
  </ol>
  
  <hr>
  
  <p><strong><em>Update</em></strong> : There is <a href="https://groups.google.com/forum/?fromgroups#!topic/jooq-user/26Bd8wnwJgQ" rel="noreferrer">a discussion</a> about this question on the JOOQ forum.</p>