Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have three basic ways to load data into objects from a JPA-based solution. These are:</p> <ol> <li>Load dynamically by object traversal (e.g. myObject.getMyCollection().get()).</li> <li>Load graphs of objects by prefetching dynamically using JPA QL (e.g. FETCH JOINs as described at <a href="http://download.oracle.com/javaee/5/tutorial/doc/bnbtl.html#bnbtq" rel="nofollow noreferrer">the Oracle JPA tutorial</a> )</li> <li>Load by setting the fetch mode ( <a href="https://stackoverflow.com/questions/1038958/is-there-a-way-to-change-the-jpa-fetch-type-on-a-method">Is there a way to change the JPA fetch type on a method?</a> )</li> </ol> <p>Each of these has pros and cons.</p> <ol> <li>Loading dynamically by object transversal will generate more (highly targeted queries). These queries are usually small (not large SQL statements, but may load lots of data) and tend to play nicely with a second level cache, but you can get lots and lots of little queries.</li> <li>Prefetching with JPA QL will give you exactly what you want, but that assumes that you know what you want.</li> <li>Setting the fetch mode to EAGER will load lots and lots of data for you automatically, but depending on the configuration and usage this may not actually help much (or may make things a lot worse) as you may wind up dragging a LOT of data from the DB into your app that you didn't expect.</li> </ol> <p>Regardless, I highly recommend using p6spy ( <a href="http://sourceforge.net/projects/p6spy/" rel="nofollow noreferrer">http://sourceforge.net/projects/p6spy/</a> ) in conjunction with any JPA-based application to understand the effects of your tuning.</p> <p>Unfortunately, JPA makes some things easy and some things hard - mainly, side-effects of your usage. For example, you might fix one problem by setting the fetch mode to eager, and then create another problem where the eager fetch pulls in too much data. EclipseLink does provide tooling to help sort this out ( <a href="http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Performance_Profiling" rel="nofollow noreferrer">EclipseLink Performance Tools</a> ) </p> <p>In theory, if you wanted to you could write a generic JavaBean property walker by using something like <a href="http://commons.apache.org/beanutils/" rel="nofollow noreferrer">Apache BeanUtils</a>. Usually just calling a method like size() on a collection is enough to force it to load (although using a collection batch fetch size might complicate things a bit).</p> <p>One thing to pay particular attention to is the scope of your session and your use of caches (<a href="http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/Caching_Overview" rel="nofollow noreferrer">EclipseLink cache</a>). </p> <p>Something not clear from your post is the scope of a session. Is a session a one shot affair (e.g. like a web page request) or is it a long running thing (e.g. like a classic client/server GUI app)?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload