title: Hibernate (JPA) how to do an eager query, loading all child objects
body: <p>Relating to my <a href="https://stackoverflow.com/questions/48733/javahibernate-jpa-designing-the-server-data-reload">earlier question</a>, I want to ensure all the child objects are loaded as I have a multiple threads that may need to access the data (and thus avoid lazy loading exceptions).  I understand the way to do this is to use the "fetch" keyword in the query (EJB QL).  Like this:</p>
  
  <pre><code>select distinct o from Order o left join fetch o.orderLines
  </code></pre>
  
  <p>Assuming a model with an <code>Order</code> class which has a set of <code>OrderLines</code> in it.</p>
  
  <p>My question is that the "distinct" keyword seems to be needed as otherwise I seem to get back an <code>Order</code> for each <code>OrderLine</code>.  Am I doing the right thing?</p>
  
  <p>Perhaps more importantly, is there a way to pull in all child objects, no matter how deep?  We have around 10-15 classes and for the server we will need everything loaded...  I was avoiding using <code>FetchType.EAGER</code> as that meant its always eager and in particular the web front end loads everything - but perhaps that is the way to go - is that what you do?  I seem to remember us trying this before and then getting really slow webpages - but perhaps that means we should be using a second-level cache?</p>