JBoss.org Community Documentation

12.7.3.1.2. D#findByPrimaryKey

Suppose we have an entity D. A typical SQL query generated for the findByPrimaryKey would look like this:

SELECT t0_D.id, t0_D.name FROM D t0_D WHERE t0_D.id=?

Suppose that while executing findByPrimaryKey we also want to preload two collection-valued CMR fields bs and cs.

<query>
    <query-method>
        <method-name>findByPrimaryKey</method-name>
        <method-params>
            <method-param>java.lang.Long</method-param>
        </method-params>
    </query-method>
    <jboss-ql><![CDATA[SELECT OBJECT(o) FROM D AS o WHERE o.id = ?1]]></jboss-ql>
    <read-ahead>
        <strategy>on-find</strategy>
        <page-size>4</page-size>
        <eager-load-group>basic</eager-load-group>
        <left-join cmr-field="bs" eager-load-group="basic"/>
        <left-join cmr-field="cs" eager-load-group="basic"/>
    </read-ahead>
</query>

The left-join declares the relations to be eager loaded. The generated SQL would look like this:

SELECT t0_D.id, t0_D.name,
       t1_D_bs.id, t1_D_bs.name,
       t2_D_cs.id, t2_D_cs.name
  FROM D t0_D
       LEFT OUTER JOIN B t1_D_bs ON t0_D.id=t1_D_bs.D_FK
       LEFT OUTER JOIN C t2_D_cs ON t0_D.id=t2_D_cs.D_FK
 WHERE t0_D.id=?

For the D with the specific id we preload all its related B's and C's and can access those instance loading them from the read ahead cache, not from the database.