JBoss.org Community Documentation Relationships

Relationships are a special case in lazy loading because a CMR field is both a field and query. As a field it can be on-load block loaded, meaning the value of the currently sought entity and the values of the CMR field for the next several entities are loaded. As a query, the field values of the related entity can be preloaded using on-find.

Again, the easiest way to investigate the loading is to look at a usage scenario. In this example, an HTML table is generated containing each gangster and their hangout. The example code follows:

public String createGangsterHangoutHtmlTable() 
    throws FinderException
    StringBuffer table = new StringBuffer();
    Collection gangsters = gangsterHome.findAll_onfind();
    for (Iterator iter = gangsters.iterator(); iter.hasNext(); ) {
        Gangster gangster = (Gangster)iter.next();

        Location hangout = gangster.getHangout();

    table.append("</table>");return table.toString();

Example 12.3. Relationship Lazy Loading Example Code

For this example, the configuration of the gangster's findAll_onfind query is unchanged from the on-find section. The configuration of the Location entity and Gangster-Hangout relationship follows:

                    <load-group-name>quick info</load-group-name>
                    <eager-load-group>quick info</eager-load-group>

Example 12.4. The jbosscmp-jdbc.xml Relationship Lazy Loading Configuration

JBoss will execute the following query for the finder:

SELECT t0_g.id, t0_g.name, t0_g.nick_name, t0_g.badness
    FROM gangster t0_g
    ORDER BY t0_g.id ASC

Then when the hangout is accessed, JBoss executes the following two queries to load the city, state, and zip fields of the hangout:

SELECT gangster.id, gangster.hangout,
       location.city, location.st, location.zip
    FROM gangster, location
    WHERE (gangster.hangout=location.id) AND
          ((gangster.id=0) OR (gangster.id=1) OR
          (gangster.id=2) OR (gangster.id=3))
SELECT gangster.id, gangster.hangout,
       location.city, location.st, location.zip
    FROM gangster, location
    WHERE (gangster.hangout=location.id) AND
          ((gangster.id=4) OR (gangster.id=5) OR
          (gangster.id=6) OR (gangster.id=7))

The following table shows the execution of the queries:

id name nick_name badness hangout id city st zip
0 Yojimbo Bodyguard 7 0 0 San Fran CA 94108
1 Takeshi Master 10 1 1 San Fran CA 94133
2 Yuriko Four finger 4 2 2 San Fran CA 94133
3 Chow Killer 9 3 3 San Fran CA 94133
4 Shogi Lightning 8 4 4 San Fran CA 94133
5 Valentino Pizza-Face 4 5 5 New York NY 10017
6 Toni Toothless 2 6 6 Chicago IL 60661
7 Corleone Godfather 6 7 7 Las Vegas NV 89109

Table 12.4. on-find Optimized Relationship Query Execution