14.2. Eager Fetching

14.2.1. Configuring Eager Fetching
14.2.2. Eager Fetching Considerations

Eager fetching is the ability to efficiently load related objects along with the objects being queried. Typically, Kodo JDO has to make a trip to the database whenever a relation is loaded. If you perform a query that returns 100 Person objects, and then you have to retrieve the Address for each person, Kodo may make as many as 101 data store queries (the initial query, plus one for the address of each person returned). With eager fetching, Kodo can reduce this to a single query.

Eager fetching only affects relations in the fetch groups being loaded. In other words, relations that would not normally be loaded immediately when retrieving an object or accessing a field are not affected by eager fetching. In our example above, the address of each person would only be eagerly fetched if the query were configured to include the address field's fetch group (or if the address were in the default fetch group). This allows you to control exactly which fields are eagerly fetched.

Eager fetching has three modes:

14.2.1. Configuring Eager Fetching

You can control Kodo's default eager fetch mode through the kodo.EagerFetchMode configuration property. Set this property to one of the mode names described in the previous section: none, single, multiple. If left unset, it defaults to multiple (assuming you have purchased the performance pack).

You can also override the default eager fetch mode on individual Kodo persistence managers, queries, and extents. All of these components give you access to their internal FetchConfiguration object for controlling object loading behavior. The runtime interfaces chapter of this manual details these interfaces, including the FetchConfiguration.

Example 14.2. Setting the Default Eager Fetch Mode

kodo.EagerFetchMode: single

Example 14.3. Setting the Eager Fetch Mode at Runtime

import kodo.runtime.*;

...

KodoQuery kq = (KodoQuery) pm.newQuery (Person.class, "address.state == \"TX\"");
kq.getFetchConfiguration ().setEagerFetchMode 
   (FetchConfiguration.EAGER_FETCH_SINGLE);
Collection results = (Collection) kq.execute ();

14.2.2. Eager Fetching Considerations

There are three important limitations of eager fetching that you should consider. First, when Kodo uses multiple selects to gather eager data under the multiple eager fetch mode, lazy result set support is turned off. Kodo will fully process each of the result sets immediately. Note that this applies only to result sets where secondary eager selects are made; if there are no to-many relations in the configured fetch groups that require separate selects, then lazy result set support will remain on, regardless of the eager fetch mode setting.

Second, eager fetching can be less efficient than standard fetching when circular relations are included in the configured fetch groups. For example, if type A has a relation to type B, and B has a relation back to A, and both relations are in the default fetch group or a configured fetch group, Kodo will be most efficient with eager fetching turned off.

Finally, when Kodo eager-fetches a 1-1 relation, any recursive eager fetching from that relation is in single mode. Kodo cannot eager-fetch a 1-1, and then recursively eager-fetch a to-many relation from that 1-1.