<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>

[{'answer_id': 71172, 'author': 'Jeremy', 'author_id': 4419, 'author_profile': 'https://Stackoverflow.com/users/4419', 'pm_score': 3, 'selected': False, 'text': '<p>I\'m not sure about using the fetch keyword in your EJBQL, you might be getting it confused with the annotation...</p>\n\n<p>Have you tried adding the FetchType property to your relationship attribute?</p>\n\n<p>@OneToMany(fetch=FetchType.EAGER)?</p>\n\n<p>See: </p>\n\n<p><a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/FetchType.html" rel="noreferrer">http://java.sun.com/javaee/5/docs/api/javax/persistence/FetchType.html</a>\n<a href="http://www.jroller.com/eyallupu/entry/hibernate_exception_simultaneously_fetch_multiple" rel="noreferrer">http://www.jroller.com/eyallupu/entry/hibernate_exception_simultaneously_fetch_multiple</a></p>\n'}, {'answer_id': 71316, 'author': 'jrudolph', 'author_id': 7647, 'author_profile': 'https://Stackoverflow.com/users/7647', 'pm_score': 0, 'selected': False, 'text': '<p>That would only work for ManyToOne relations and for them @ManyToOne(fetch=FetchType.EAGER) would probably appropriate.</p>\n\n<p>Fetching more than one OneToMany relation eagerly is discouraged and/or does not work as you can read in the link Jeremy posted. Just think about the SQL statement that would be needed to do such a fetch...</p>\n'}, {'answer_id': 71499, 'author': 'James Law', 'author_id': 11855, 'author_profile': 'https://Stackoverflow.com/users/11855', 'pm_score': 5, 'selected': True, 'text': "<p>Changing the annotation is a bad idea IMO. As it can't be changed to lazy at runtime. Better to make everything lazy, and fetch as needed.</p>\n\n<p>I'm not sure I understand your problem without mappings. Left join fetch should be all you need for the use case you describe. Of course you'll get back an order for every orderline if orderline has an order as its parent.</p>\n"}, {'answer_id': 73267, 'author': 'Chris Kimpton', 'author_id': 48310, 'author_profile': 'https://Stackoverflow.com/users/48310', 'pm_score': 0, 'selected': False, 'text': '<p>What I have done is to refactor the code to keep a map of objects to entity managers and each time I need to refresh, close the old entitymanager for the object and open a new one.  I used the above query without the <strong>fetch</strong> as that is going too deep for my needs - just doing a plain join pulls in the OrderLines - the <strong>fetch</strong> makes it go even deeper.  </p>\n\n<p>There are only a few objects that I need this for, around 20, so I think the resource overhead in having 20 open entitymanagers is not an issue - although the DBAs may have a different view when this goes live...</p>\n\n<p>I also re-worked things so that the db work is on the main thread and has the entity manager.  </p>\n\n<p>Chris</p>\n'}, {'answer_id': 74284, 'author': 'ncgz', 'author_id': 12905, 'author_profile': 'https://Stackoverflow.com/users/12905', 'pm_score': -1, 'selected': False, 'text': '<p>If the problem is just LazyInitializationExceptions, you can avoid that by adding an OpenSessionInViewFilter.<br>\nThis will allow the objects to be loaded in the view, but will not help with the speed issue.</p>\n\n<pre><code>     &lt;filter&gt;\n        &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;\n        &lt;filter-class&gt; org.springframework.orm.hibernate3.support.OpenSessionInViewFilter\n        &lt;/filter-class&gt;\n    &lt;/filter&gt;\n    &lt;filter-mapping&gt;\n        &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;\n        &lt;url-pattern&gt;/*&lt;/url-pattern&gt;\n    &lt;/filter-mapping&gt;\n</code></pre>\n'}, {'answer_id': 75459, 'author': 'Mike Desjardins', 'author_id': 10466, 'author_profile': 'https://Stackoverflow.com/users/10466', 'pm_score': 2, 'selected': False, 'text': '<p>You might be able to do something like that using a (detached) criteria query, and setting the fetch mode.  E.g.,</p>\n\n<pre><code>Session s = ((HibernateEntityManager) em).getSession().getSessionFactory().openSession();\nDetachedCriteria dc = DetachedCriteria.forClass(MyEntity.class).add(Expression.idEq(id));\ndc.setFetchMode("innerTable", FetchMode.JOIN);\nCriteria c = dc.getExecutableCriteria(s);\nMyEntity a = (MyEntity)c.uniqueResult();\n</code></pre>\n'}, {'answer_id': 227780, 'author': 'Miguel Ping', 'author_id': 22992, 'author_profile': 'https://Stackoverflow.com/users/22992', 'pm_score': 2, 'selected': False, 'text': '<p>Have you tried using a result transformer? If you use Criteria queries, you can apply a result transformer (although <a href="http://www.jroller.com/RickHigh/entry/hibernate_query_getting_rid_of" rel="nofollow noreferrer">there are some problems with pagination and result transformer</a>):</p>\n\n<pre><code>Criteria c = ((Session)em.getDelegate()).createCriteria(Order.class);\nc.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);\nc.list();\n</code></pre>\n\n<p>the <code>em.getDelegate()</code> is a hack that only works if you are using hibernate.</p>\n\n<blockquote>\n  <p>Perhaps more importantly, is there a\n  way to pull in all child objects, no\n  matter how deep? We have around 10-15\n  classes and for the server we will\n  need everything loaded... I was\n  avoiding using FetchType.EAGER as that\n  meant its always eager and in\n  particular the web front end loads\n  everything - but perhaps that is the\n  way to go - is that what you do? I\n  seem to remember us trying this before\n  and then getting really slow webpages\n  - but perhaps that means we should be using a second-level cache?</p>\n</blockquote>\n\n<p>If you are still interested, I responded a similar question in this thread <a href="https://stackoverflow.com/questions/182323/how-to-serialize-hibernate-collections-properly#182955">how to serialize hibernate collections</a>.</p>\n\n<p>Basically you use a utility called <a href="http://dozer.sf.net" rel="nofollow noreferrer">dozer</a> that maps beans onto another beans, and by doing this you trigger all your lazy loads. As you can imagine, this works better if all collections are eagerly fetched.</p>\n'}]
['https://Stackoverflow.com/questions/70992', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/48310/']