EclipseLink/UserGuide/sandbox/gelernter/fetch

From Eclipsepedia

Jump to: navigation, search


From "Using EclipseLink JPA Extensions (ELUG)" - http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29 JoinFetch




From "Optimizing the EclipseLink Application (ELUG)" - http://wiki.eclipse.org/Optimizing_the_EclipseLink_Application_%28ELUG%29 under

11 Optimizing Queries

11.4 How to Use Partial Object Queries and Fetch Groups for Optimization


Contents

How to Use Partial Object Queries and Fetch Groups for Optimization

Partial object queries let you retrieve partially populated objects from the database rather than complete objects.

When using weaving with JPA or POJO applications, you can use fetch groups to accomplish the same performance optimization.

For more information about partial object reading, see Partial Object Queries.

For more information about fetch groups, see Fetch Groups.


Also, later: under "Reading Case 1: Displaying Names in a List"

Fetch Groups

Fetch groups 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 EclipseLink 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.

The Configuring a Query with a FetchGroup Using the FetchGroupManager example demonstrates the use of a static fetch group.


Configuring a Query with a FetchGroup Using the FetchGroupManager

JPA

// Use fetch group at query level
ReadAllQuery query = new ReadAllQuery(Employee.class);
FetchGroup group = new FetchGroup("nameOnly");
group.addAttribute("firstName");
group.addAttribute("lastName");
query.setFetchGroup(group);
 
JpaQuery jpaQuery = (JpaQuery)entityManager.createQuery("Select e from Employee e");
jpaQuery.setDatabaseQuery(query);
 
List employees = jpaQuery.getResultList();
 
 
/* Only Employee attributes firstName and lastName are fetched.
   If you call the Employee get method for any other attribute, EclipseLink executes
   another query to retrieve all unfetched attribute values. Thereafter,
   calling that get method will return the value directly from the object */


Native API

// Use fetch group at query level
ReadAllQuery query = new ReadAllQuery(Employee.class);
FetchGroup group = new FetchGroup("nameOnly");
group.addAttribute("firstName");
group.addAttribute("lastName");
query.setFetchGroup(group);
 
List employees = session.executeQuery(query);
 
/* Only Employee attributes firstName and lastName are fetched.
   If you call the Employee get method for any other attribute, EclipseLink executes
   another query to retrieve all unfetched attribute values. Thereafter,
   calling that get method will return the value directly from the object */





from "Using Advanced Query API (ELUG)" - http://wiki.eclipse.org/Using_Advanced_Query_API_%28ELUG%29

Using Queries with Fetch Groups

You can use a fetch group with a ReadObjectQuery or ReadAllQuery. When you execute the query, EclipseLink retrieves only the attributes in the fetch group. EclipseLink 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.


Note: When you use fetch groups outside of CMP, use weaving (see Using Weaving).


This section describes the following:

For more information about fetch groups, see Fetch Groups and Object-Level Read Queries.


How to Configure Default Fetch Group Behavior

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, EclipseLink will use the default fetch group unless you configure the query otherwise, as this example shows.


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


How to Query with a Static Fetch Group

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, EclipseLink 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");


How to Query with a Dynamic Fetch Group

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.

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


Configuring a Query with a FetchGroup Dynamically


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