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

From Eclipsepedia

Jump to: navigation, search
m
m (What You May Need to Know About EclipseLink JPA Lazy Loading)
Line 4: Line 4:
 
You are not required to provide value for this attribute.
 
You are not required to provide value for this attribute.
  
==What You May Need to Know About EclipseLink JPA Lazy Loading==
+
===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.
  
JPA specifies that lazy loading is a hint to the persistence provider that data should be fetched lazily when it is first accessed, if possible.
+
If you execute a <tt>ReadObjectQuery</tt> or <tt>ReadAllQuery</tt> without specifying a fetch group, EclipseLink will use the default fetch group unless you configure the query otherwise, as this example shows.
  
If you are developing your application in a Java EE environment, you only have to set fetch to <tt>javax.persistence.FetchType.LAZY</tt>, and EclipseLink persistence provider will supply all the necessary functionality.
 
  
When using a [[Introduction%20to%20EclipseLink%20JPA%20(ELUG)#@OneToOne|one-to-one]] or [[Introduction%20to%20EclipseLink%20JPA%20(ELUG)#@ManyToOne|many-to-one]] mapping in a Java SE environment, to configure EclipseLink JPA to perform lazy loading when the <tt>fetch</tt> attribute is set to <tt>FetchType.LAZY</tt>, configure either dynamic or static weaving.
+
<span id="Example 107-3"></span>
 
+
''''' Configuring Default Fetch Group Behavior'''''
When using a [[Introduction%20to%20EclipseLink%20JPA%20(ELUG)#@OneToOne|one-to-one]] or [[Introduction%20to%20EclipseLink%20JPA%20(ELUG)#@ManyToOne|many-to-one]] mapping in a Java SE environment that does not permit the use of <tt>-javaagent</tt> on the JVM command line, to configure EclipseLink JPA to perform lazy loading when annotation attribute <tt>fetch</tt> is set to <tt>javax.persistence.FetchType.LAZY</tt>, you can use static weaving.
+
'''// at the descriptor level'''
 
+
FetchGroup group = new FetchGroup("nameOnly");
The [[#Table 19-33|EclipseLink JPA Support for Lazy Loading by Mapping Type]] table lists EclipseLink JPA support for lazy loading by mapping type.
+
group.addAttribute("firstName");
 
+
group.addAttribute("lastName");
For more information, see the following:
+
employeeDescriptor.getFetchGroupManager().addFetchGroup(group);
* [[#How to Configure Dynamic Weaving for JPA Entities Using the EclipseLink Agent|How to Configure Dynamic Weaving for JPA Entities Using the EclipseLink Agent]]
+
'''// set the default fetch group'''
* [[#How to Configure Static Weaving for JPA Entities|How to Configure Static Weaving for JPA Entities]]
+
employeeDescriptor.getFetchGroupManager().setDefaultFetchGroup(group);
* [[#How to Disable Weaving Using EclipseLink Persistence Unit Properties|How to Disable Weaving Using EclipseLink Persistence Unit Properties]]
+
* [[#What You May Need to Know About Weaving JPA Entities|What You May Need to Know About Weaving JPA Entities]]
+
'''// when query1 is executed, the default fetch group applies'''
* [[Configuring%20a%20Mapping%20(ELUG)#Configuring Indirection (Lazy Loading)|Configuring Indirection (Lazy Loading)]]
+
ReadAllQuery query1 = new ReadAllQuery(Employee.class);
 
+
 
+
'''// when query2 is executed, the default fetch group does not apply'''
<span id="Table 19-33"></span>
+
ReadAllQuery query2 = new ReadAllQuery(Employee.class);
''''' EclipseLink JPA Support for Lazy Loading by Mapping Type'''''
+
query2.setShouldUsedefaultFetchGroup(false);
 
+
{| class="RuleFormalWideMax" dir="ltr" title="EclipseLink JPA Support for Lazy Loading by Mapping Type" summary="This table lists EclipseLink JPA mapping support for lazy loading by mapping type for Java EE (in container) and Java SE (outside of container) applications." width="100%" border="1" frame="border" rules="all" cellpadding="3" frame="border" rules="all"
+
|- align="left" valign="top"
+
! id="r1c1-t52" align="left" valign="bottom" | '''Mapping'''
+
! id="r1c2-t52" align="left" valign="bottom" | '''Java EE''' <sup> 1 </sup>
+
! id="r1c3-t52" align="left" valign="bottom" | '''Java SE'''
+
|- align="left" valign="top"
+
| id="r2c1-t52" headers="r1c1-t52" align="left" |
+
[[Introduction%20to%20EclipseLink%20JPA%20(ELUG)#@ManyToMany|many-to-many]]
+
| headers="r2c1-t52 r1c2-t52" align="left" |
+
EclipseLink JPA performs lazy loading when the <tt>fetch</tt> attribute is set to <tt>javax.persistence.FetchType.LAZY</tt> (default).
+
| headers="r2c1-t52 r1c3-t52" align="left" |
+
EclipseLink JPA performs lazy loading when the <tt>fetch</tt> attribute is set to <tt>javax.persistence.FetchType.LAZY</tt> (default).
+
|- align="left" valign="top"
+
| id="r3c1-t52" headers="r1c1-t52" align="left" |
+
[[Introduction%20to%20EclipseLink%20JPA%20(ELUG)#@OneToMany|one-to-many]]
+
| headers="r3c1-t52 r1c2-t52" align="left" |
+
EclipseLink JPA performs lazy loading when the <tt>fetch</tt> attribute is set to <tt>javax.persistence.FetchType.LAZY</tt> (default).
+
| headers="r3c1-t52 r1c3-t52" align="left" |
+
EclipseLink JPA performs lazy loading when the <tt>fetch</tt> attribute is set to <tt>javax.persistence.FetchType.LAZY</tt> (default).
+
|- align="left" valign="top"
+
| id="r4c1-t52" headers="r1c1-t52" align="left" |
+
[[Introduction%20to%20EclipseLink%20JPA%20(ELUG)#@OneToOne|one-to-one]]
+
| headers="r4c1-t52 r1c2-t52" align="left" |
+
EclipseLink JPA performs lazy loading when the <tt>fetch</tt> attribute is set to <tt>javax.persistence.FetchType.LAZY</tt>.
+
| headers="r4c1-t52 r1c3-t52" align="left" |
+
By default, EclipseLink JPA ignores the <tt>fetch</tt> attribute and default javax.persistence.<tt>FetchType.EAGER</tt> applies.
+
 
+
To configure EclipseLink JPA to perform lazy loading when the <tt>fetch</tt> attribute set to <tt>FetchType.LAZY</tt>, consider one of the following:
+
* [[#How to Configure Dynamic Weaving for JPA Entities Using the EclipseLink Agent|How to Configure Dynamic Weaving for JPA Entities Using the EclipseLink Agent]]
+
* [[#How to Configure Static Weaving for JPA Entities|How to Configure Static Weaving for JPA Entities]]
+
|- align="left" valign="top"
+
| id="r5c1-t52" headers="r1c1-t52" align="left" |
+
[[Introduction%20to%20EclipseLink%20JPA%20(ELUG)#@ManyToOne|many-to-one]]
+
| headers="r5c1-t52 r1c2-t52" align="left" |
+
EclipseLink JPA performs lazy loading when the <tt>fetch</tt> attribute is set to <tt>javax.persistence.FetchType.LAZY</tt>.
+
| headers="r5c1-t52 r1c3-t52" align="left" |
+
By default, EclipseLink JPA ignores the <tt>fetch</tt> attribute and default <tt>javax.persistence.FetchType.EAGER</tt> applies.
+
 
+
To configure EclipseLink JPA to perform lazy loading when the <tt>fetch</tt> attribute set to <tt>FetchType.LAZY</tt>, configure one of the following:
+
* [[#How to Configure Dynamic Weaving for JPA Entities Using the EclipseLink Agent|How to Configure Dynamic Weaving for JPA Entities Using the EclipseLink Agent]]
+
* [[#How to Configure Static Weaving for JPA Entities|How to Configure Static Weaving for JPA Entities]]
+
|- align="left" valign="top"
+
| id="r6c1-t52" headers="r1c1-t52" align="left" |
+
[[Introduction%20to%20EclipseLink%20JPA%20(ELUG)#@Basic|basic]]
+
| headers="r6c1-t52 r1c2-t52" align="left" |
+
EclipseLink JPA performs lazy loading when the <tt>fetch</tt> attribute is set to <tt>javax.persistence.FetchType.LAZY</tt>.
+
| headers="r6c1-t52 r1c3-t52" align="left" |
+
By default, EclipseLink JPA ignores the <tt>fetch</tt> attribute and default javax.persistence.<tt>FetchType.EAGER</tt> applies.
+
 
+
To configure EclipseLink JPA to perform lazy loading when the <tt>fetch</tt> attribute set to <tt>FetchType.LAZY</tt>, consider one of the following:
+
* [[#How to Configure Dynamic Weaving for JPA Entities Using the EclipseLink Agent|How to Configure Dynamic Weaving for JPA Entities Using the EclipseLink Agent]]
+
* [[#How to Configure Static Weaving for JPA Entities|How to Configure Static Weaving for JPA Entities]]
+
|}
+
<br><sup> 1 </sup>Fully supported in any container that implements the appropriate container contracts in the EJB 3.0 specification.<br>
+
 
+
{{EclipseLink_JPA
+
|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]]
+
|up=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Relationship_Mappings/Common_Relationship_Configurations|Common Relationship Configurations]]}}
+

Revision as of 12:27, 16 June 2010

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