title: Open Session In View Pattern
body: <p>I'm asking this question given my chosen development frameworks of JPA (Hibernate implementation of), Spring, and &lt;insert MVC framework here - Struts 1, Struts 2, Spring MVC, Stripes...&gt;.</p>
  
  <p>I've been thinking a bit about relationships in my entity layer - for example I have an order entity that has many order lines. I've set up my app so that it eagerly loads the order lines for every order. Do you think this is a lazy way to get around the lazy initialization problems that I would come across if I was to set the fetch strategy to false?</p>
  
  <p>The way I see it, I have the following alternatives when retrieving entities and their associations:</p>
  
  <ol>
  <li><p>Use the Open Session In View pattern to create the session on each request and commit the transaction before returning the response.</p></li>
  <li><p>Implement a DTO (Data Transfer Object) layer such that every DAO query I execute returns the correctly initialized DTO for my purposes. I don't really like this option much because in my experience I've found that it creates a lot of boilerplate copying code and becomes messy to maintain.</p></li>
  <li><p>Don't map any associations in JPA so that every query I execute returns only the entities I'm interested in - this will probably require me to have DTOs anyway and will be a pain to maintain and I think defeats the purpose of having an ORM in the first place.</p></li>
  <li><p>Eagerly fetch all (or most associations) - in the example above, always fetch all order lines when I retrieve an order. </p></li>
  </ol>
  
  <p>So my question is, when and under what circumstances would you use which of these options? Do you always stick with one way of doing it?</p>
  
  <p>I would ask a colleague but I think that if I even mentioned the term 'Open Session in View' I would be greeted with blank stares :( What I'm really looking for here is some advice from a senior or very experienced developer.</p>
  
  <p>Thanks guys!</p>