Oracle TopLink Developer's Guide
10g Release 3 (10.1.3) B13593-01 |
|
![]() Previous |
![]() Next |
You can use a fetch group with a ReadObjectQuery
or ReadAllQuery
. When you execute the query, TopLink retrieves only the attributes in the fetch group. TopLink automatically executes a query to fetch all the attributes excluded from this subset when and if you call a getter method on any one of the excluded attributes.
This section describes the following:
For more information about fetch groups, see "Fetch Groups and Object Level Read Queries".
You can optionally designate at most one fetch group as the default fetch group for a descriptor's reference class.
If you execute a ReadObjectQuery
or ReadAllQuery
without specifying a fetch group, TopLink will use the default fetch group unless you configure the query otherwise, as Example 99-3 shows.
Example 99-3 Configuring Default Fetch Group Behavior
// at the descriptor level FetchGroup group = new FetchGroup("nameOnly"); group.addAttribute("firstName"); group.addAttribute("lastName"); employeeDescriptor.getFetchGroupManager().addFetchGroup(group); // set the default fetch group employeeDescriptor.getFetchGroupManager().setDefaultFetchGroup(group); // when query1 is executed, the default fetch group applies ReadAllQuery query1 = new ReadAllQuery(Employee.class); // when query2 is executed, the default fetch group does not apply ReadAllQuery query2 = new ReadAllQuery(Employee.class); query2.setShouldUsedefaultFetchGroup(false);
Example 99-4 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.
Example 99-4 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");
Example 99-5 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.
Example 99-5 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);