Jump to: navigation, search

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

(Batch Reading)
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.
 
EclipseLink allows users the option to optimize their queries using batch and joined reading through the use of a query hint.
 
== Via Annotations ==
 
  
 
== Batch Reading ==
 
== Batch Reading ==
 
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
  
 
=== Via Annotations ===
 
=== Via Annotations ===
Line 17: Line 15:
 
     @QueryHint(
 
     @QueryHint(
 
       name=EclipseLinkQueryHints.BATCH,  
 
       name=EclipseLinkQueryHints.BATCH,  
       value="e.manager.phoneNumbers"),
+
       value="e.manager.phoneNumbers")
 +
  }
 +
)
 +
public class Employee implements Serializable {
 +
...
 +
 
 +
}
 +
</pre></code>
 +
 
 +
== Via XML ==
 +
<code><pre>
 +
<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.batch" value="e.manager.phoneNumbers">
 +
  </named-query>
 +
  ...
 +
</entity>
 +
</pre></code>
 +
 
 +
== Joined Reading ==
 +
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 allow 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 ===
 +
<code><pre>
 +
@Entity
 +
@Table(name="JPA_EMPLOYEE")
 +
@NamedQuery(
 +
  name="findEmployeeByPK",
 +
  query="SELECT OBJECT(employee) FROM Employee employee WHERE employee.id = :id"),
 +
  hints={
 
     @QueryHint(
 
     @QueryHint(
 
       name=EclipseLinkQueryHints.FETCH,  
 
       name=EclipseLinkQueryHints.FETCH,  
       value=EclipseLinkQueryHints.FETCH),
+
       value="e.manager.phoneNumbers")
 
   }
 
   }
 
)
 
)
Line 35: Line 65:
 
   <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>
 
</pre></code>

Revision as of 16:00, 15 October 2007

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

Batch Reading

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

Via Annotations

@Entity
@Table(name="JPA_EMPLOYEE")
@NamedQuery(
  name="findEmployeeByPK",
  query="SELECT OBJECT(employee) FROM Employee employee WHERE employee.id = :id"),
  hints={
    @QueryHint(
      name=EclipseLinkQueryHints.BATCH, 
      value="e.manager.phoneNumbers")
  }
)
public class Employee implements Serializable {
...

}

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.batch" value="e.manager.phoneNumbers">
  </named-query>
  ...
</entity>

Joined Reading

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 allow 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

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

}

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>