Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Relationship Mappings/Common Relationship Configurations/JoinFetch"

m (How to Configure Default Fetch Group Behavior)
m
Line 8: Line 8:
 
You are not required to provide value for this attribute.
 
You are not required to provide value for this attribute.
  
===How to Configure Default Fetch Group Behavior===
+
==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.
 
You can optionally designate at most one fetch group as the default fetch group for a descriptor's reference class.
  
Line 17: Line 17:
 
''''' Configuring Default Fetch Group Behavior'''''
 
''''' Configuring Default Fetch Group Behavior'''''
 
<source lang="java">
 
<source lang="java">
  '''// at the descriptor level'''
+
  // at the descriptor level
 
  FetchGroup group = new FetchGroup("nameOnly");
 
  FetchGroup group = new FetchGroup("nameOnly");
 
  group.addAttribute("firstName");
 
  group.addAttribute("firstName");
 
  group.addAttribute("lastName");
 
  group.addAttribute("lastName");
 
  employeeDescriptor.getFetchGroupManager().addFetchGroup(group);
 
  employeeDescriptor.getFetchGroupManager().addFetchGroup(group);
  '''// set the default fetch group'''
+
  // set the default fetch group'''
 
  employeeDescriptor.getFetchGroupManager().setDefaultFetchGroup(group);
 
  employeeDescriptor.getFetchGroupManager().setDefaultFetchGroup(group);
 
   
 
   
  '''// when query1 is executed, the default fetch group applies'''
+
  // when query1 is executed, the default fetch group applies
 
  ReadAllQuery query1 = new ReadAllQuery(Employee.class);
 
  ReadAllQuery query1 = new ReadAllQuery(Employee.class);
 
   
 
   
  '''// when query2 is executed, the default fetch group does not apply'''
+
  // when query2 is executed, the default fetch group does not apply
 
  ReadAllQuery query2 = new ReadAllQuery(Employee.class);
 
  ReadAllQuery query2 = new ReadAllQuery(Employee.class);
 
  query2.setShouldUsedefaultFetchGroup(false);
 
  query2.setShouldUsedefaultFetchGroup(false);
 
</source>
 
</source>
  
===How to Query with a Static Fetch Group===
+
==How to Query with a Static Fetch Group==
  
 
[[#Example 107-4|Configuring a Query with a FetchGroup Using the FetchGroupManager]] shows how to configure a <tt>ReadObjectQuery</tt> for the <tt>Employee</tt> class with a <tt>FetchGroup</tt> named <tt>nameOnly</tt> previously stored in the <tt>FetchGroupManager</tt> owned by the <tt>Employee</tt> class's descriptor.
 
[[#Example 107-4|Configuring a Query with a FetchGroup Using the FetchGroupManager]] shows how to configure a <tt>ReadObjectQuery</tt> for the <tt>Employee</tt> class with a <tt>FetchGroup</tt> named <tt>nameOnly</tt> previously stored in the <tt>FetchGroupManager</tt> owned by the <tt>Employee</tt> class's descriptor.
Line 43: Line 43:
 
In this example, only the <tt>Employee</tt> attributes <tt>firstName</tt> and <tt>lastName</tt> are fetched. If you call the <tt>Employee</tt> method <tt>get</tt> for any other attribute, EclipseLink executes another query to retrieve all unfetched attribute values. Thereafter, calling that <tt>get</tt> method will return the value directly from the object.
 
In this example, only the <tt>Employee</tt> attributes <tt>firstName</tt> and <tt>lastName</tt> are fetched. If you call the <tt>Employee</tt> method <tt>get</tt> for any other attribute, EclipseLink executes another query to retrieve all unfetched attribute values. Thereafter, calling that <tt>get</tt> method will return the value directly from the object.
  
+
<source lang="java">
  '''// create static fetch group at the descriptor level'''
+
  // create static fetch group at the descriptor level
 
  FetchGroup group = new FetchGroup("nameOnly");
 
  FetchGroup group = new FetchGroup("nameOnly");
 
  group.addAttribute("firstName");
 
  group.addAttribute("firstName");
Line 50: Line 50:
 
  descriptor.getFetchGroupManager().addFetchGroup(group);
 
  descriptor.getFetchGroupManager().addFetchGroup(group);
 
   
 
   
  '''// use static fetch group at query level'''
+
  // use static fetch group at query level
 
  ReadAllQuery query = new ReadAllQuery(Employee.class);
 
  ReadAllQuery query = new ReadAllQuery(Employee.class);
 
  query.setFetchGroupName("nameOnly");
 
  query.setFetchGroupName("nameOnly");
 +
</source>
  
 
+
==How to Query with a Dynamic Fetch Group==
===How to Query with a Dynamic Fetch Group===
+
  
 
[[#Example 107-5|Configuring a Query with a FetchGroup Dynamically]] shows how to create a <tt>FetchGroup</tt> instance dynamically, at the time you create and execute a query, and configure the query with that <tt>FetchGroup</tt> directly.
 
[[#Example 107-5|Configuring a Query with a FetchGroup Dynamically]] shows how to create a <tt>FetchGroup</tt> instance dynamically, at the time you create and execute a query, and configure the query with that <tt>FetchGroup</tt> directly.
Line 65: Line 65:
 
''''' Configuring a Query with a FetchGroup Dynamically'''''
 
''''' Configuring a Query with a FetchGroup Dynamically'''''
  
+
<source lang="java">
  '''// dynamic fetch group query'''
+
  // dynamic fetch group query
 
  ReadAllQuery query = new ReadAllQuery(Employee.class);
 
  ReadAllQuery query = new ReadAllQuery(Employee.class);
 
  FetchGroup group = new FetchGroup("nameAndSalary");
 
  FetchGroup group = new FetchGroup("nameAndSalary");
Line 73: Line 73:
 
  group.addAttribute("salary");
 
  group.addAttribute("salary");
 
  query. setFetchGroup(group);
 
  query. setFetchGroup(group);
 
+
</source>
  
  
Line 79: Line 79:
 
|previous= [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Relationship_Mappings/Common_Relationship_Configurations|Common Relationship Configurations]]
 
|previous= [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Relationship_Mappings/Common_Relationship_Configurations|Common Relationship Configurations]]
 
|next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Collection_Mappings|Collection Mappings]]
 
|next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Collection_Mappings|Collection Mappings]]
|up=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Relationship_Mappings/Common_Relationship_Configurations|Common Relationship Configurations]]}}
+
|up=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Relationship_Mappings/Common_Relationship_Configurations|Common Relationship Configurations]]
 +
|version=2.1.0}}

Revision as of 12:02, 17 June 2010


Elug api package icon.png Key API {{{apis}}}

Fetch Type

By default, EclipseLink persistence provider uses a fetch type of javax.persitence.FetchType.EAGER: this is a requirement on the persistence provider runtime that data must be eagerly fetched.If the default is inappropriate for your application or a particular persistent field, set fetch to FetchType.LAZY: this is a hint to the persistence provider that data should be fetched lazily when it is first accessed (if possible). We recommend using the FetchType.LAZY on all relationships.

You are not required to provide value for this attribute.

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


Eclipselink-logo.gif
Version: 2.1.0
Other versions...