Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

m (Fetch Type)
m
 
(23 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=Fetch Type=
+
{{EclipseLink_UserGuide
By default, EclipseLink persistence provider uses a fetch type of <tt>javax.persitence.FetchType.EAGER:</tt> 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 <tt>FetchType.LAZY:</tt> 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 <tt>FetchType.LAZY</tt> on all relationships.
+
|info=y
 +
|toc=n
 +
|eclipselink=y
 +
|eclipselinktype=JPA
  
You are not required to provide value for this attribute.
+
|api=y
 +
|apis=
 +
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/JoinFetch.html @JoinFetch]
  
==What You May Need to Know About EclipseLink JPA Lazy Loading==
+
}}
 +
=@JoinFetch=
  
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.
+
Use the <tt>@JoinFetch</tt> annotation to enable the joining and reading of the related objects in the same query as the source object.
 +
{{EclipseLink_Note
 +
|note=We recommend setting join fetching at the query level, as not all queries require joining.
 +
}}
  
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.
+
You can specify the <tt>@JoinFetch</tt> annotation for the following mappings:
  
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.
+
* <tt>@OneToOne</tt>
 +
* <tt>[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Collection_Mappings/OneToMany|@OneToMany]]</tt>
 +
* <tt>@ManyToOne</tt>
 +
* <tt>[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Collection_Mappings/ManyToMany|@ManyToMany]]</tt>
 +
* <tt>@BasicCollection</tt> (deprecated)
 +
* <tt>@BasicMap</tt> (deprecated)
  
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.
 
  
The [[#Table 19-33|EclipseLink JPA Support for Lazy Loading by Mapping Type]] table lists EclipseLink JPA support for lazy loading by mapping type.
+
Alternatively, you can use batch reading, especially for collection relationships.]].
  
For more information, see the following:
+
{{EclipseLink_AttributeTable
* [[#How to Configure Dynamic Weaving for JPA Entities Using the EclipseLink Agent|How to Configure Dynamic Weaving for JPA Entities Using the EclipseLink Agent]]
+
|caption=@JoinFetch Annotation Attributes
* [[#How to Configure Static Weaving for JPA Entities|How to Configure Static Weaving for JPA Entities]]
+
|content=<tr>
* [[#How to Disable Weaving Using EclipseLink Persistence Unit Properties|How to Disable Weaving Using EclipseLink Persistence Unit Properties]]
+
<td>'''<tt>value</tt>'''</td>
* [[#What You May Need to Know About Weaving JPA Entities|What You May Need to Know About Weaving JPA Entities]]
+
<td>Set this attribute to the <tt>org.eclipse.persistence.annotations.JoinFetchType</tt> enumerated type of the fetch that you will be using.
* [[Configuring%20a%20Mapping%20(ELUG)#Configuring Indirection (Lazy Loading)|Configuring Indirection (Lazy Loading)]]
+
  
 +
The following are the valid values for the <tt>JoinFetchType</tt><nowiki>:</nowiki>
 +
* <tt>INNER</tt> – This option provides the inner join fetching of the related object.<br>Note: Inner joining does not allow for null or empty values.
 +
* <tt>OUTER</tt> – This option provides the outer join fetching of the related object.<br>Note: Outer joining allows for null or empty values.<br>
  
<span id="Table 19-33"></span>
 
''''' EclipseLink JPA Support for Lazy Loading by Mapping Type'''''
 
  
{| 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"
+
</td>
|- align="left" valign="top"
+
<td><tt>JoinFetchType.INNER</tt></td>
! id="r1c1-t52" align="left" valign="bottom" | '''Mapping'''
+
<td>No</td>
! id="r1c2-t52" align="left" valign="bottom" | '''Java EE''' <sup> 1 </sup>
+
</tr>
! 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:
+
The following example shows how to use the <tt>@JoinFetch</tt> annotation to specify <tt>Employee</tt> field <tt>managedEmployees</tt>.
* [[#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]]
+
<span id="Example: @JoinFetch Annotation"></span>
|- align="left" valign="top"
+
======''Example: @JoinFetch Annotation''======
| id="r6c1-t52" headers="r1c1-t52" align="left" |
+
<source lang="java">
[[Introduction%20to%20EclipseLink%20JPA%20(ELUG)#@Basic|basic]]
+
@Entity
| headers="r6c1-t52 r1c2-t52" align="left" |
+
public class Employee implements Serializable {
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" |
+
    @OneToMany(cascade=ALL, mappedBy="owner")
By default, EclipseLink JPA ignores the <tt>fetch</tt> attribute and default javax.persistence.<tt>FetchType.EAGER</tt> applies.
+
    @JoinFetch(value=OUTER)
 +
    public Collection<Employee> getManagedEmployees() {
 +
        return managedEmployees;
 +
    }
 +
    ...
 +
}
 +
</source>
 +
 
  
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
 
{{EclipseLink_JPA
|previous= [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Relationship_Mappings/Common_Relationship_Configuration|Common Relationship Configuration]]
+
|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_Configuration|Common Relationship Configuration]]}}
+
|up=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Relationship_Mappings/Common_Relationship_Configurations|Common Relationship Configurations]]
 +
|version=2.2.0 DRAFT}}

Latest revision as of 12:07, 17 May 2011

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source

Elug api package icon.png Key API

@JoinFetch

Use the @JoinFetch annotation to enable the joining and reading of the related objects in the same query as the source object.

Elug note icon.png

Note: We recommend setting join fetching at the query level, as not all queries require joining.

You can specify the @JoinFetch annotation for the following mappings:


Alternatively, you can use batch reading, especially for collection relationships.]].

@JoinFetch Annotation Attributes
Attribute Description Default Required?
value Set this attribute to the org.eclipse.persistence.annotations.JoinFetchType enumerated type of the fetch that you will be using.

The following are the valid values for the JoinFetchType:

  • INNER – This option provides the inner join fetching of the related object.
    Note: Inner joining does not allow for null or empty values.
  • OUTER – This option provides the outer join fetching of the related object.
    Note: Outer joining allows for null or empty values.


JoinFetchType.INNER No


The following example shows how to use the @JoinFetch annotation to specify Employee field managedEmployees.

Example: @JoinFetch Annotation
 @Entity
 public class Employee implements Serializable {
     ...
     @OneToMany(cascade=ALL, mappedBy="owner")
     @JoinFetch(value=OUTER)
     public Collection<Employee> getManagedEmployees() {
         return managedEmployees;
     }
     ...
 }


Eclipselink-logo.gif
Version: 2.2.0 DRAFT
Other versions...

Back to the top