Difference between revisions of "EclipseLink/Examples/JPA/QueryOptimization"

From Eclipsepedia

Jump to: navigation, search
(Via Annotations)
 
(18 intermediate revisions by 4 users not shown)
Line 1: Line 1:
EclipseLink allows users the option to optimize their queries using batch and joined reading through the use of a query hint.
+
[[Category:EclipseLink/Example/JPA|QueryOptimization]]
  
== Via Annotations ==
+
EclipseLink gives users the option to optimize their queries using batch and joined reading through the use of a query hints.
  
=== Batch Reading ===
+
See:
 +
* [[Optimizing_the_EclipseLink_Application_%28ELUG%29#Optimizing_Queries|Optimizing Queries]] in the ''[[EclipseLink/UserGuide|EclipseLink User's Guide]]''
 +
* [[Using_EclipseLink_JPA_Extensions_(ELUG)#Using_EclipseLink_JPA_Query_Customization_Extensions|Using EclipseLink JPA Query Customization Extensions]] in the ''[[EclipseLink/UserGuide|EclipseLink User's Guide]]''
 +
* [[Optimizing_the_EclipseLink_Application_%28ELUG%29#How_to_Use_Batch_and_Join_Reading_for_Optimization|How to Use Batch and Join Reading]] in the ''[[EclipseLink/UserGuide|EclipseLink User's Guide]]''
 +
* [http://www.eclipse.org/eclipselink/api/1.2/org/eclipse/persistence/config/QueryHints.html org.eclipse.persistence.config.QueryHints]
 +
* Batch fetch example - [http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/trunk/examples/org.eclipse.persistence.example.jpa.batch-fetch/batch-fetch.zip download link] - [http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/trunk/examples/org.eclipse.persistence.example.jpa.batch-fetch/ SVN]
 +
 
 +
* [http://java-persistence-performance.blogspot.com/2010/08/batch-fetching-optimizing-object-graph.html Blog: Batch fetching - optimizing object graph loading]
 +
 
 +
 
 +
== Batch Reading (Fetching) ==
 
Configures the query to optimize the retrieval of the related objects, the related objects for all the resulting objects will be read in a single query (instead of n queries). Valid values are strings that represent JPQL style navigations to a relationship.
 
Configures the query to optimize the retrieval of the related objects, the related objects for all the resulting objects will be read in a single query (instead of n queries). Valid values are strings that represent JPQL style navigations to a relationship.
<p>e.g. e.manager.phoneNumbers
+
e.g. e.manager.phoneNumbers
  
<code><pre>
+
=== Via Annotations ===
 +
<source lang="java">
 
@Entity
 
@Entity
 
@Table(name="JPA_EMPLOYEE")
 
@Table(name="JPA_EMPLOYEE")
 
@NamedQuery(
 
@NamedQuery(
   name="findEmployeeByPK",
+
   name="findAllEmployees",
   query="SELECT OBJECT(employee) FROM Employee employee WHERE employee.id = :id"),
+
   query="SELECT e FROM Employee e order by e.id"),
 
   hints={
 
   hints={
 
     @QueryHint(
 
     @QueryHint(
       name=EclipseLinkQueryHints.BATCH,  
+
       name=QueryHints.BATCH,  
       value="e.manager.phoneNumbers"),
+
       value="e.manager.phoneNumbers")
 +
  }
 +
)
 +
public class Employee implements Serializable {
 +
...
 +
 
 +
}
 +
</source>
 +
 
 +
=== Via XML ===
 +
<source lang="xml">
 +
<entity name="Employee" class="org.eclipse.testing.Employee" access="PROPERTY">
 +
  ...
 +
  <named-query name="findAllEmployees">
 +
    <query>SELECT e FROM Employee e order by e.id</query>
 +
    <hint name="eclipselink.batch" value="e.manager.phoneNumbers">
 +
  </named-query>
 +
  ...
 +
</entity>
 +
</source>
 +
 
 +
== Joined Reading (Fetching) ==
 +
Configures the query to optimize the retrieval of the related objects, the related objects will be joined into the query instead of being queried independently. This allows for nested join fetching which is not supported in JPQL. Valid values are strings that represent JPQL style navigations to a relationship.
 +
e.g. e.manager.phoneNumbers
 +
 
 +
=== Via Annotations ===
 +
<source lang="java">
 +
@Entity
 +
@Table(name="JPA_EMPLOYEE")
 +
@NamedQuery(
 +
  name="findAllEmployeesByFirstName",
 +
  query="SELECT OBJECT(employee) FROM Employee employee WHERE employee.firstName = :firstname"),
 +
  hints={
 
     @QueryHint(
 
     @QueryHint(
       name=EclipseLinkQueryHints.FETCH,  
+
       name=QueryHints.FETCH,  
       value=EclipseLinkQueryHints.FETCH),
+
       value="e.manager.phoneNumbers")
 
   }
 
   }
 
)
 
)
Line 26: Line 69:
  
 
}
 
}
</pre></code>
+
</source>
  
== Via XML ==
+
=== Via XML ===
<code><pre>
+
<source lang="xml">
 
<entity name="Employee" class="org.eclipse.testing.Employee" access="PROPERTY">
 
<entity name="Employee" class="org.eclipse.testing.Employee" access="PROPERTY">
 
   ...
 
   ...
 
   <named-query name="findAllEmployeesByFirstName">
 
   <named-query name="findAllEmployeesByFirstName">
 
     <query>SELECT OBJECT(employee) FROM Employee employee WHERE employee.firstName = :firstname</query>
 
     <query>SELECT OBJECT(employee) FROM Employee employee WHERE employee.firstName = :firstname</query>
    <hint name="eclipselink.batch" value="eclipselink.batch">
+
     <hint name="eclipselink.join-fetch" value="e.manager.phoneNumbers">
     <hint name="eclipselink.join-fetch" value="eclipselink.join-fetch">
+
 
   </named-query>
 
   </named-query>
 
   ...
 
   ...
 
</entity>
 
</entity>
</pre></code>
+
</source>

Latest revision as of 13:54, 31 January 2013


EclipseLink gives users the option to optimize their queries using batch and joined reading through the use of a query hints.

See:


Contents

[edit] Batch Reading (Fetching)

Configures the query to optimize the retrieval of the related objects, the related objects for all the resulting objects will be read in a single query (instead of n queries). Valid values are strings that represent JPQL style navigations to a relationship. e.g. e.manager.phoneNumbers

[edit] Via Annotations

@Entity
@Table(name="JPA_EMPLOYEE")
@NamedQuery(
  name="findAllEmployees",
  query="SELECT e FROM Employee e order by e.id"),
  hints={
    @QueryHint(
      name=QueryHints.BATCH, 
      value="e.manager.phoneNumbers")
  }
)
public class Employee implements Serializable {
...
 
}

[edit] Via XML

<entity name="Employee" class="org.eclipse.testing.Employee" access="PROPERTY">
  ...
  <named-query name="findAllEmployees">
    <query>SELECT e FROM Employee e order by e.id</query>
    <hint name="eclipselink.batch" value="e.manager.phoneNumbers">
  </named-query>
  ...
</entity>

[edit] Joined Reading (Fetching)

Configures the query to optimize the retrieval of the related objects, the related objects will be joined into the query instead of being queried independently. This allows for nested join fetching which is not supported in JPQL. Valid values are strings that represent JPQL style navigations to a relationship. e.g. e.manager.phoneNumbers

[edit] Via Annotations

@Entity
@Table(name="JPA_EMPLOYEE")
@NamedQuery(
  name="findAllEmployeesByFirstName",
  query="SELECT OBJECT(employee) FROM Employee employee WHERE employee.firstName = :firstname"),
  hints={
    @QueryHint(
      name=QueryHints.FETCH, 
      value="e.manager.phoneNumbers")
  }
)
public class Employee implements Serializable {
...
 
}

[edit] Via XML

<entity name="Employee" class="org.eclipse.testing.Employee" access="PROPERTY">
  ...
  <named-query name="findAllEmployeesByFirstName">
    <query>SELECT OBJECT(employee) FROM Employee employee WHERE employee.firstName = :firstname</query>
    <hint name="eclipselink.join-fetch" value="e.manager.phoneNumbers">
  </named-query>
  ...
</entity>