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

From Eclipsepedia

Jump to: navigation, search
m (How to Configure Default Fetch Group Behavior)
m
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{EclipseLink_Toc}}
+
{{EclipseLink_UserGuide
{{EclipseLink_API
+
|info=y
|1=javax.persitence.FetchType
+
|toc=n
}}
+
|eclipselink=y
=Fetch Type=
+
|eclipselinktype=JPA
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.
+
  
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]
  
===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.
+
=@JoinFetch=
  
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.
+
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.
 +
}}
  
 +
You can specify the <tt>@JoinFetch</tt> annotation for the following mappings:
  
<span id="Example 107-3"></span>
+
* <tt>@OneToOne</tt>
''''' Configuring Default Fetch Group Behavior'''''
+
* <tt>[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Collection_Mappings/OneToMany|@OneToMany]]</tt>
<source lang="java">
+
* <tt>@ManyToOne</tt>
'''// at the descriptor level'''
+
* <tt>[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Collection_Mappings/ManyToMany|@ManyToMany]]</tt>
FetchGroup group = new FetchGroup("nameOnly");
+
* <tt>@BasicCollection</tt> (deprecated)
group.addAttribute("firstName");
+
* <tt>@BasicMap</tt> (deprecated)
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);
+
</source>
+
  
===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.
+
Alternatively, you can use batch reading, especially for collection relationships.]].
  
 +
{{EclipseLink_AttributeTable
 +
|caption=@JoinFetch Annotation Attributes
 +
|content=<tr>
 +
<td>'''<tt>value</tt>'''</td>
 +
<td>Set this attribute to the <tt>org.eclipse.persistence.annotations.JoinFetchType</tt> enumerated type of the fetch that you will be using.
  
<span id="'Example 107-4"></span>
+
The following are the valid values for the <tt>JoinFetchType</tt><nowiki>:</nowiki>
'''' Configuring a Query with a FetchGroup Using the FetchGroupManager'''''
+
* <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>
  
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.
 
  
   
+
  </td>
  '''// create static fetch group at the descriptor level'''
+
  <td><tt>JoinFetchType.INNER</tt></td>
  FetchGroup group = new FetchGroup("nameOnly");
+
  <td>No</td>
group.addAttribute("firstName");
+
</tr>
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===
+
The following example shows how to use the <tt>@JoinFetch</tt> annotation to specify <tt>Employee</tt> field <tt>managedEmployees</tt>.
  
[[#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.
+
<span id="Example: @JoinFetch Annotation"></span>
 
+
======''Example: @JoinFetch Annotation''======
In this example, only the <tt>firstName</tt>, <tt>lastName</tt>, and <tt>salary</tt> attributes 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">
 
+
  @Entity
 
+
  public class Employee implements Serializable {
<span id="Example 107-5"></span>
+
    ...
''''' Configuring a Query with a FetchGroup Dynamically'''''
+
    @OneToMany(cascade=ALL, mappedBy="owner")
 
+
    @JoinFetch(value=OUTER)
   
+
    public Collection<Employee> getManagedEmployees() {
  '''// dynamic fetch group query'''
+
        return managedEmployees;
ReadAllQuery query = new ReadAllQuery(Employee.class);
+
    }
FetchGroup group = new FetchGroup("nameAndSalary");
+
    ...
group.addAttribute("firstName");
+
  }
group.addAttribute("lastName");
+
</source>
group.addAttribute("salary");
+
  query. setFetchGroup(group);
+
  
  
Line 79: Line 69:
 
|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.2.0 DRAFT}}

Latest revision as of 12:07, 17 May 2011

EclipseLink JPA

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

Elug api package icon.png Key API

[edit] @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.

[edit] 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...