Skip Headers

# Fetch Groups

Previous

Next

Fetch groups, applicable only to CMP projects, are similar to partial object reading, but does allow caching of the objects read. For objects with many attributes or reference attributes to complex graphs (or both), you can define a fetch group that determines what attributes are returned when an object is read. Because TopLink will automatically execute additional queries when the `get` method is called for attributes not in the fetch group, ensure that the unfetched data is not required: refetching data can become a performance issue.

For more information about querying with fetch groups, see "Using Queries With Fetch Groups".

Example: Configuring a Query with a FetchGroup Using the FetchGroupManager demonstrates the use of a static fetch group.

Configuring a Query with a FetchGroup Using the FetchGroupManager

    // Create static fetch group at the descriptor level
    FetchGroup group = new FetchGroup("nameOnly");
    group.addAttribute("firstName");
    group.addAttribute("lastName");
    descriptor.getFetchGroupManager().addFetchGroup(group);

    // Use static fetch group at query level
    ReadAllQuery query = new ReadAllQuery(Employee.class);
    query.setFetchGroupName("nameOnly");

    // Only Employee attributes firstName and lastName are fetched.
    // If you call the Employee get method for any other attribute, TopLink executes
    // another query to retrieve all unfetched attribute values. Thereafter, calling that get
    // method will return the value directly from the object