Skip Headers

# Querying With a Static Fetch Group

Previous

Next

Example: Configuring a Query with a FetchGroup Using the FetchGroupManager shows how to configure a `ReadObjectQuery` for the `Employee` class with a `FetchGroup` named `nameOnly` previously stored in the `FetchGroupManager` owned by the `Employee` class's descriptor.

Configuring a Query with a FetchGroup Using the FetchGroupManager

In this example, only the `Employee` attributes `firstName` and `lastName` are fetched. If you call the `Employee` method `get` 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.

    // 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");