Skip Headers

# Querying With a Dynamic Fetch Group

Previous

Next

Example: Configuring a Query with a FetchGroup Dynamically shows how to create a `FetchGroup` instance dynamically, at the time you create and execute a query, and configure the query with that `FetchGroup` directly.

Configuring a Query with a FetchGroup Dynamically

In this example, only the `firstName`, `lastName`, and `salary` attributes 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.

    // dynamic fetch group query
    ReadAllQuery query = new ReadAllQuery(Employee.class);
    FetchGroup group = new FetchGroup("nameAndSalary");
    group.addAttribute("firstName");
    group.addAttribute("lastName");
    group.addAttribute("salary");
    query. setFetchGroup(group);