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/Querying/Query Hints"

m
m (EclipseLink JPA Query Hints)
Line 1: Line 1:
 
=EclipseLink JPA Query Hints=
 
=EclipseLink JPA Query Hints=
 +
The [[#Table 19-30|EclipseLink JPA Query Hints]] table lists the EclipseLink JPA query hints that you can specify when you construct a JPA query, as the [[#Example 19-24|Specifying an EclipseLink JPA Query Hint]] example shows, or when you specify a JPA query using the <tt>@QueryHint</tt> annotation, as the [[#Example 19-25|Specifying an EclipseLink JPA Query Hint with @QueryHint]] example shows.
 +
{{EclispeLink_Spec
 +
|section=Section 8.3.1 "NamedQuery Annotation"}}
  
 +
All EclipseLink query hints are defined in the <tt>QueryHints</tt> class in the <tt>org.eclipse.persistence.config</tt> package.
 +
 +
The EclipseLink query hints include the following:
 +
* <tt>[[#Cache Usage|eclipselink.cache-usage]]</tt>
 +
* <tt>[[#Query Type|eclipselink.query-type]]</tt>
 +
* <tt>[[#Bind Parameters|eclipselink.jdbc.bind-parameters]]</tt>
 +
* <tt>[[#Pessimistic Lock|eclipselink.pessimistic-lock]]</tt>
 +
* <tt>[[#Refresh|eclipselink.refresh]]</tt>
 +
* <tt>[[#Refresh|eclipselink.refresh.cascade]]</tt>
 +
* <tt>[[#Maintain Cache|eclipselink.maintain-cache]]</tt>
 +
* <tt>[[#Batch|eclipselink.batch]]</tt>
 +
* <tt>[[#Join Fetch|eclipselink.join-fetch]]</tt>
 +
* <tt>[[#Read Only|eclipselink.read-only]]</tt>
 +
* <tt>[[#Timeout|eclipselink.jdbc.timeout]]</tt>
 +
* <tt>[[#Fetch Size|eclipselink.jdbc.fetch-size]]</tt>
 +
* <tt>[[#Max Rows|eclipselink.jdbc.max-rows]]</tt>
 +
* <tt>[[#Result Collection Type|eclipselink.result-collection-type]]</tt>
 +
 +
When you set a hint, you can set the value using the public static final field in the appropriate configuration class in <tt>org.eclipse.persistence.config</tt> package, including the following:
 +
* <tt>HintValues</tt>
 +
* <tt>CacheUsage</tt>
 +
* <tt>PessimisticLock</tt>
 +
* <tt>QueryType</tt>
 +
 +
The [[#Example 19-24|Specifying an EclipseLink JPA Query Hint]] and [[#Example 19-25|Specifying an EclipseLink JPA Query Hint with @QueryHint]] examples show how to set the value of hint <tt>eclipselink.jdbc.bind-parameters</tt> using the <tt>QueryHints</tt> configuration class to set the name of the hint, and the <tt>HintValues</tt> configuration class to set the value.
 +
 +
<span id="Example 19-24"></span>
 +
''''' Specifying an EclipseLink JPA Query Hint'''''
 +
<source lang="java">
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
 +
Customer customer = (Customer)entityMgr.createNamedQuery("findCustomerBySSN").
 +
    setParameter("SSN", "123-12-1234").
 +
    setHint(QueryHints.BIND_PARAMETERS, HintValues.PERSISTENCE_UNIT_DEFAULT).
 +
    getSingleResult();
 +
</source>
 +
 +
<span id="Example 19-25"></span>
 +
''''' Specifying an EclipseLink JPA Query Hint with @QueryHint'''''
 +
<source lang="java">
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
 +
@Entity
 +
@NamedQuery(
 +
    name="findEmployeeByDept",
 +
    query="SELECT e FROM Employee e WHERE e.dept=:deptNum",
 +
    hints=@QueryHint(name=QueryHints.BIND_PARAMETERS, value=HintValues.TRUE)
 +
)
 +
public class Employee implements Serializable {
 +
    ...
 +
}
 +
</source>
 +
 +
====Cache Usage====
 +
The <tt>eclipselink.cache-usage</tt> hint specifies how the query should interact with the EclipseLink cache.
 +
 +
EclipseLink JPA uses a shared cache mechanism that is scoped to the entire persistence unit. When operations are completed in a particular persistence context, the results are merged back into the shared cache so that other persistence contexts can use them. This happens regardless of whether the entity manager and persistence context are created in Java SE or Java EE. Any entity persisted or removed using the entity manager will always be kept consistent with the cache.
 +
 +
For more information, see the following:
 +
* [[Introduction%20to%20Cache%20(ELUG)#Session Cache|Session Cache]]
 +
* [[Introduction%20to%20EclipseLink%20Queries%20(ELUG)#How to Use In-Memory Queries|How to Use In-Memory Queries]]
 +
 +
The following are the valid values for the <tt>org.eclipse.persistence.config.CacheUsage</tt><nowiki>:</nowiki>
 +
* <tt>DoNotCheckCache</tt> – Always go to the database.
 +
* <tt>CheckCacheByExactPrimaryKey</tt> – If a read-object query contains an expression where the primary key is the only comparison, you can obtain a cache hit if you process the expression against the object in memory
 +
* <tt>CheckCacheByPrimaryKey</tt> – If a read-object query contains an expression that compares at least the primary key, you can obtain a cache hit if you process the expression against the objects in memory.
 +
* <tt>CheckCacheThenDatabase</tt> – You can configure any read-object query to check the cache completely before you resort to accessing the database.
 +
* <tt>CheckCacheOnly</tt> – You can configure any read-all query to check only the parent session cache (shared cache) and return the result from it without accessing the database.
 +
* <tt>ConformResultsInUnitOfWork</tt> – You can configure any read-object or read-all query within the context of a unit of work to conform the results with the changes to the object made within that unit of work. This includes new objects, deleted objects and changed objects.<br>For more information, see [[Using%20Advanced%20Unit%20of%20Work%20API%20(ELUG)#Using Conforming Queries and Descriptors|Using Conforming Queries and Descriptors]].
 +
* <tt>UseEntityDefault</tt> – Use the cache configuration as specified by the EclipseLink descriptor API for this entity.<br>Note: the entity default value is to not check the cache (<tt>DoNotCheckCache</tt>). The query will access the database and synchronize with the cache. Unless refresh has been set on the query, the cached objects will be returned without being refreshed from the database. EclipseLink does not support the cache usage for native queries or queries that have complex result sets such as returning data or multiple objects.
 +
 +
Default:
 +
<tt>CacheUsage.UseEntityDefault</tt><br>Note: this default is <tt>DoNotCheckCache</tt>.
 +
 +
For more information, see [[Introduction%20to%20EclipseLink%20Queries%20(ELUG)#Configuring Cache Usage for In-Memory Queries|Configuring Cache Usage for In-Memory Queries]].
 +
 +
 +
'''Example'''<nowiki>: JPA Query API</nowiki>
 +
<source lang="java">
 +
import org.eclipse.persistence.config.CacheUsage;
 +
import org.eclipse.persistence.config.QueryHints;
 +
query.setHint(QueryHints.CACHE_USAGE, CacheUsage.CheckCacheOnly);
 +
 +
'''Example'''<nowiki>: </nowiki><tt>@QueryHint</tt>
 +
import org.eclipse.persistence.config.CacheUsage;
 +
import org.eclipse.persistence.config.TargetDatabase;
 +
@QueryHint(name=QueryHints.CACHE_USAGE, value=CacheUsage.CheckCacheOnly);
 +
</source>
 +
 +
====Query Type====
 +
The <tt>eclipselink.query-type</tt> hint specifies the EclipseLink query type to use for the query.
 +
 +
For most JP QL queries, the <tt>org.eclipse.persistence.queries.ReportQuery</tt> or <tt>org.eclipse.persistence.queries.ReadAllQuery</tt> are used. The <tt>eclipselink.query-type</tt> hint lets you use other query types, such as <tt>org.eclipse.persistence.queries.ReadObjectQuery</tt> for queries that are know to return a single object.
 +
 +
For more information, see the following:
 +
* [[Introduction%20to%20EclipseLink%20Queries%20(ELUG)#Object-Level Read Query|Object-Level Read Query]]
 +
* [[Introduction%20to%20EclipseLink%20Queries%20(ELUG)#Data-Level Modify Query|Data-Level Modify Query]]
 +
 +
The following are the valid values for the <tt>org.eclipse.persistence.config.QueryType</tt><nowiki>:</nowiki>
 +
* <tt>Auto</tt> – EclipseLink chooses the type of query to use.
 +
* <tt>ReadAll</tt> – Use the [[Introduction%20to%20EclipseLink%20Queries%20(ELUG)#ReadAllQuery|ReadAllQuery]] type for the query.
 +
* <tt>ReadObject</tt> – Use the [[Introduction%20to%20EclipseLink%20Queries%20(ELUG)#|ReadObjectQuery]] type for the query.
 +
* <tt>Report</tt> – Use the [[Introduction%20to%20EclipseLink%20Queries%20(ELUG)#Report Query|Report Query]] type for the query.
 +
 +
Default: <tt>QueryType.Auto</tt>
 +
 +
'''Example'''<nowiki>: JPA Query API</nowiki>
 +
<source lang="java">
 +
import org.eclipse.persistence.config.QueryType;
 +
import org.eclipse.persistence.config.QueryHints;
 +
query.setHint(QueryHints.QUERY_TYPE, QueryType.ReadObject);
 +
</source>
 +
 +
'''Example'''<nowiki>: </nowiki><tt>@QueryHint</tt>
 +
<source lang="java">
 +
import org.eclipse.persistence.config.QueryType;
 +
import org.eclipse.persistence.config.TargetDatabase;
 +
@QueryHint(name=QueryHints.QUERY_TYPE, value=QueryType.ReadObject);
 +
</source>
 +
 +
====Bind Parameters====
 +
The <tt>eclipselink.jdbc.bind-parameters</tt> hint controls whether or not the query uses parameter binding.
 +
 +
For more information, see [[Optimizing%20the%20EclipseLink%20Application%20(ELUG)#How to Use Parameterized SQL (Parameter Binding) and Prepared Statement Caching for Optimization|How to Use Parameterized SQL (Parameter Binding) and Prepared Statement Caching for Optimization]].
 +
 +
The following are the valid values for the <tt>org.eclipse.persistence.config.HintValues</tt><nowiki>:</nowiki>
 +
* <tt>TRUE</tt> – bind all parameters.
 +
* <tt>FALSE</tt> – do not bind all parameters.
 +
* <tt>PERSISTENCE_UNIT_DEFAULT</tt> – use the parameter binding setting made in your EclipseLink session's database login, which is <tt>true</tt> by default.<br><br>For more information, see [[Configuring%20a%20Database%20Login%20(ELUG)#Configuring JDBC Options|Configuring JDBC Options]].
 +
 +
Default: <tt>HintValues.PERSISTENCE_UNIT_DEFAULT</tt>
 +
 +
'''Example'''<nowiki>: JPA Query API</nowiki>
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
query.setHint(QueryHints.BIND_PARAMETERS, HintValues.TRUE);
 +
 +
'''Example'''<nowiki>: </nowiki><tt>@QueryHint</tt>
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.TargetDatabase;
 +
@QueryHint(name=QueryHints.BIND_PARAMETERS, value=HintValues.TRUE);
 +
 +
====Fetch Size====
 +
The <tt>eclipselink.jdbc.fetch-size</tt> hint specifies the number of rows that should be fetched from the database when more rows are needed<sup>1</sup>
 +
 +
For large queries that return a large number of objects you can configure the row fetch size used in the query to improve performance by reducing the number database hits required to satisfy the selection criteria. Most JDBC drivers default to a fetch size of 10, so if you are reading 1000 objects, increasing the fetch size to 256 can significantly reduce the time required to fetch the query's results. The optimal fetch size is not always obvious. Usually, a fetch size of one half or one quarter of the total expected result size is optimal. Note that if you are unsure of the result set size, incorrectly setting a fetch size too large or too small can decrease performance.
 +
 +
A value of 0 means the JDBC driver default will be used.
 +
 +
Valid values: 0 to <tt>Integer.MAX_VALUE</tt> (depending on your JDBC driver) as a <tt>String</tt>.
 +
 +
Default: 0
 +
 +
Note: this value indicates that the JDBC driver default will be used.
 +
 +
<br><sup>1</sup> This property is dependent on the JDBC driver support.
 +
 +
====Timeout====
 +
The <tt>eclipselink.jdbc.timeout</tt> hint specifies the number of seconds EclipseLink will wait on a query before throwing a <tt>DatabaseException</tt><sup>1</sup>
 +
 +
A value of 0 means EclipseLink will never time-out a query.
 +
 +
Valid values: 0 to <tt>Integer.MAX_VALUE</tt> (depending on your JDBC driver) as a <tt>String</tt>.
 +
 +
Default: 0
 +
 +
<br><sup>1</sup> This property is dependent on the JDBC driver support.
 +
 +
====Pessimistic Lock====
 +
The <tt>eclipselink.pessimistic-lock</tt> hint controls whether or not pessimistic locking is used.
 +
 +
The following are the valid values for the <tt>org.eclipse.persistence.config.PessimisticLock</tt><nowiki>:</nowiki>
 +
* <tt>NoLock</tt> – pessimistic locking is not used.
 +
* <tt>Lock</tt> – EclipseLink issues a <tt>SELECT .... FOR UPDATE</tt>.
 +
* <tt>LockNoWait</tt> – EclipseLink issues a <tt>SELECT .... FOR UPDATE NO WAIT</tt>.
 +
 +
Default: <tt>NoLock</tt>
 +
 +
'''Example'''<nowiki>: JPA Query API</nowiki>
 +
import org.eclipse.persistence.config.PessimisticLock;
 +
import org.eclipse.persistence.config.QueryHints;
 +
query.setHint(QueryHints.PESSIMISTIC_LOCK, PessimisticLock.LockNoWait);
 +
 +
'''Example'''<nowiki>: </nowiki><tt>@QueryHint</tt>
 +
import org.eclipse.persistence.config.PessimisticLock;
 +
import org.eclipse.persistence.config.QueryHints;
 +
@QueryHint(name=QueryHints.PESSIMISTIC_LOCK, value=PessimisticLock.LockNoWait);
 +
 +
====Batch====
 +
The <tt>eclipselink.batch</tt> hint supplies EclipseLink with batching information so subsequent queries of related objects can be optimized in batches instead of being retrieved one-by-one or in one large joined read. Batch reading is more efficient than joining because it avoids reading duplicate data.
 +
 +
Batching is only allowed on queries that have a single object in their select clause.
 +
 +
Valid values: a single-valued relationship path expression.
 +
 +
Note: use dot notation to access nested attributes. For example, to batch-read an employee's manager's address, specify <tt>e.manager.address</tt>
 +
 +
 +
'''Example'''<nowiki>: JPA Query API</nowiki>
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
query.setHint("eclipselink.batch", "e.address");
 +
'''Example'''<nowiki>: </nowiki><tt>@QueryHint</tt>
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
@QueryHint(name=QueryHints.BATCH, value="e.address");
 +
 +
====Join Fetch====
 +
The <tt>eclipselink.join-fetch</tt> hint allows joining of the attributes.
 +
 +
This is similar to <tt>eclipselink.batch</tt><nowiki>: subsequent queries of related objects can be optimized in batches instead of being retrieved in one large joined read.</nowiki>
 +
 +
This is different from JP QL joining because it allows multilevel fetch joins.
 +
 +
For more information, see Section 4.4.5.3 "Fetch Joins" of JPA specification.
 +
 +
Valid values: a relationship path expression.
 +
 +
Note: use dot notation to access nested attributes.
 +
 +
 +
'''Example'''<nowiki>: JPA Query API</nowiki>
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
query.setHint("eclipselink.join-fetch", "e.address");
 +
 +
'''Example'''<nowiki>: </nowiki><tt>@QueryHint</tt>
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
@QueryHint(name=QueryHints.FETCH, value="e.address");
 +
 +
====Refresh====
 +
The <tt>eclipselink.refresh</tt> hint controls whether or not to update the EclipseLink session cache with objects that the query returns.
 +
 +
The following are the valid values for the <tt>org.eclipse.persistence.config.HintValues</tt><nowiki>:</nowiki>
 +
* <tt>TRUE</tt> – refresh cache.
 +
* <tt>FALSE</tt> – do not refresh cache.
 +
 +
Default: <tt>FALSE</tt>
 +
 +
'''Example'''<nowiki>: JPA Query API</nowiki>
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
query.setHint(QueryHints.REFRESH, HintValues.TRUE);
 +
 +
'''Example'''<nowiki>: </nowiki><tt>@QueryHint</tt>
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
@QueryHint(name=QueryHints.REFRESH, value=HintValues.TRUE);
 +
 +
====Maintain Cache====
 +
The <tt>eclipselink.maintain-cache</tt> hint controls whether or not query results are cached in the session cache and provides a way to query the current database contents without affecting the current persistence context.  It configures the query to return unmanaged instances so any updates to entities queried using this hint would have to be merged into the persistence context.
 +
 +
The following are the valid values for the <tt>org.eclipse.persistence.config.HintValues</tt><nowiki>:</nowiki>
 +
* <tt>TRUE</tt> – maintain cache.
 +
* <tt>FALSE</tt> – do not maintain cache.
 +
 +
Default: <tt>FALSE</tt>
 +
 +
'''Example'''<nowiki>: JPA Query API</nowiki>
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
query.setHint(QueryHints.MAINTAIN_CACHE, HintValues.FALSE);
 +
 +
'''Example'''<nowiki>: </nowiki><tt>@QueryHint</tt>
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
@QueryHint(name=QueryHints.MAINTAIN_CACHE, value=HintValues.FALSE);
 +
 +
====Read Only====
 +
The <tt>eclipselink.read-only</tt> hint retrieves read-only results back from the query: on nontransactional read operations, where the requested entity types are stored in the shared cache, you can request that the shared instance be returned instead of a detached copy.
 +
 +
Note: you should never modify objects returned from the shared cache.
 +
 +
The following are the valid values for the <tt>org.eclipse.persistence.config.HintValues</tt><nowiki>:</nowiki>
 +
* <tt>TRUE</tt> – retrieve read-only results back from the query;
 +
* <tt>FALSE</tt> – do not retrieve read-only results back from the query.
 +
 +
Default: <tt>FALSE</tt>
 +
 +
'''Example'''<nowiki>: JPA Query API</nowiki>
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
query.setHint(QueryHints.READ_ONLY, HintValues.TRUE);
 +
 +
'''Example'''<nowiki>: </nowiki><tt>@QueryHint</tt>
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
@QueryHint(name=QueryHints.READ_ONLY, value=HintValues.TRUE);
 +
 +
====Result Collection Type====
 +
The <tt>eclipselink.result-collection-type</tt> hint configures the concrete class that EclipseLink should use to return its query result.
 +
 +
This lets you specify the type of collection in which the result will be returned.
 +
 +
Valid values: Java <tt>Class</tt> that implements the <tt>Collection</tt> interface.
 +
 +
Note: typically, you would execute these queries by calling the <tt>getResultsList</tt> method, which returns the java.util.List, on the <tt>Query</tt>. This means that the class specified in this hint must implement the <tt>List</tt> interface, if you are invoking it using the <tt>getResultsList</tt> method.
 +
 +
Note: specify the class without the <tt>".class"</tt> notation. For example, <tt>java.util.Vector</tt> would work, not <tt>java.util.Vector.class</tt>EclipseLink will throw an exception, if you use this hint with a class that does not implement the <tt>Collection</tt> interface.
 +
 +
Default:<tt>java.util.Vector</tt>
 +
 +
'''Example'''<nowiki>: JPA Query API</nowiki>
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
query.setHint("eclipselink.result-collection-type", java.util.ArrayList.class);
 +
 +
'''Example'''<nowiki>: </nowiki><tt>@QueryHint</tt>
 +
import org.eclipse.persistence.config.HintValues;
 +
import org.eclipse.persistence.config.QueryHints;
 +
@QueryHint(name=QueryHints.RESULT_COLLECTION_TYPE, value="java.util.ArrayList");
  
  

Revision as of 12:04, 18 June 2010

EclipseLink JPA Query Hints

The EclipseLink JPA Query Hints table lists the EclipseLink JPA query hints that you can specify when you construct a JPA query, as the Specifying an EclipseLink JPA Query Hint example shows, or when you specify a JPA query using the @QueryHint annotation, as the Specifying an EclipseLink JPA Query Hint with @QueryHint example shows. Template:EclispeLink Spec

All EclipseLink query hints are defined in the QueryHints class in the org.eclipse.persistence.config package.

The EclipseLink query hints include the following:

When you set a hint, you can set the value using the public static final field in the appropriate configuration class in org.eclipse.persistence.config package, including the following:

  • HintValues
  • CacheUsage
  • PessimisticLock
  • QueryType

The Specifying an EclipseLink JPA Query Hint and Specifying an EclipseLink JPA Query Hint with @QueryHint examples show how to set the value of hint eclipselink.jdbc.bind-parameters using the QueryHints configuration class to set the name of the hint, and the HintValues configuration class to set the value.

Specifying an EclipseLink JPA Query Hint

 import org.eclipse.persistence.config.HintValues;
 import org.eclipse.persistence.config.QueryHints;
 
 Customer customer = (Customer)entityMgr.createNamedQuery("findCustomerBySSN").
     setParameter("SSN", "123-12-1234").
     setHint(QueryHints.BIND_PARAMETERS, HintValues.PERSISTENCE_UNIT_DEFAULT).
     getSingleResult();

Specifying an EclipseLink JPA Query Hint with @QueryHint

 import org.eclipse.persistence.config.HintValues;
 import org.eclipse.persistence.config.QueryHints;
 
 @Entity
 @NamedQuery(
     name="findEmployeeByDept",
     query="SELECT e FROM Employee e WHERE e.dept=:deptNum",
     hints=@QueryHint(name=QueryHints.BIND_PARAMETERS, value=HintValues.TRUE)
 )
 public class Employee implements Serializable {
     ...
 }

Cache Usage

The eclipselink.cache-usage hint specifies how the query should interact with the EclipseLink cache.

EclipseLink JPA uses a shared cache mechanism that is scoped to the entire persistence unit. When operations are completed in a particular persistence context, the results are merged back into the shared cache so that other persistence contexts can use them. This happens regardless of whether the entity manager and persistence context are created in Java SE or Java EE. Any entity persisted or removed using the entity manager will always be kept consistent with the cache.

For more information, see the following:

The following are the valid values for the org.eclipse.persistence.config.CacheUsage:

  • DoNotCheckCache – Always go to the database.
  • CheckCacheByExactPrimaryKey – If a read-object query contains an expression where the primary key is the only comparison, you can obtain a cache hit if you process the expression against the object in memory
  • CheckCacheByPrimaryKey – If a read-object query contains an expression that compares at least the primary key, you can obtain a cache hit if you process the expression against the objects in memory.
  • CheckCacheThenDatabase – You can configure any read-object query to check the cache completely before you resort to accessing the database.
  • CheckCacheOnly – You can configure any read-all query to check only the parent session cache (shared cache) and return the result from it without accessing the database.
  • ConformResultsInUnitOfWork – You can configure any read-object or read-all query within the context of a unit of work to conform the results with the changes to the object made within that unit of work. This includes new objects, deleted objects and changed objects.
    For more information, see Using Conforming Queries and Descriptors.
  • UseEntityDefault – Use the cache configuration as specified by the EclipseLink descriptor API for this entity.
    Note: the entity default value is to not check the cache (DoNotCheckCache). The query will access the database and synchronize with the cache. Unless refresh has been set on the query, the cached objects will be returned without being refreshed from the database. EclipseLink does not support the cache usage for native queries or queries that have complex result sets such as returning data or multiple objects.

Default: CacheUsage.UseEntityDefault
Note: this default is DoNotCheckCache.

For more information, see Configuring Cache Usage for In-Memory Queries.


Example: JPA Query API

 import org.eclipse.persistence.config.CacheUsage;
 import org.eclipse.persistence.config.QueryHints;
 query.setHint(QueryHints.CACHE_USAGE, CacheUsage.CheckCacheOnly);
 
'''Example'''<nowiki>: </nowiki><tt>@QueryHint</tt> 
 import org.eclipse.persistence.config.CacheUsage;
 import org.eclipse.persistence.config.TargetDatabase;
 @QueryHint(name=QueryHints.CACHE_USAGE, value=CacheUsage.CheckCacheOnly);

Query Type

The eclipselink.query-type hint specifies the EclipseLink query type to use for the query.

For most JP QL queries, the org.eclipse.persistence.queries.ReportQuery or org.eclipse.persistence.queries.ReadAllQuery are used. The eclipselink.query-type hint lets you use other query types, such as org.eclipse.persistence.queries.ReadObjectQuery for queries that are know to return a single object.

For more information, see the following:

The following are the valid values for the org.eclipse.persistence.config.QueryType:

  • Auto – EclipseLink chooses the type of query to use.
  • ReadAll – Use the ReadAllQuery type for the query.
  • ReadObject – Use the ReadObjectQuery type for the query.
  • Report – Use the Report Query type for the query.

Default: QueryType.Auto

Example: JPA Query API

 import org.eclipse.persistence.config.QueryType;
 import org.eclipse.persistence.config.QueryHints;
 query.setHint(QueryHints.QUERY_TYPE, QueryType.ReadObject);

Example: @QueryHint

 import org.eclipse.persistence.config.QueryType;
 import org.eclipse.persistence.config.TargetDatabase;
 @QueryHint(name=QueryHints.QUERY_TYPE, value=QueryType.ReadObject);

Bind Parameters

The eclipselink.jdbc.bind-parameters hint controls whether or not the query uses parameter binding.

For more information, see How to Use Parameterized SQL (Parameter Binding) and Prepared Statement Caching for Optimization.

The following are the valid values for the org.eclipse.persistence.config.HintValues:

  • TRUE – bind all parameters.
  • FALSE – do not bind all parameters.
  • PERSISTENCE_UNIT_DEFAULT – use the parameter binding setting made in your EclipseLink session's database login, which is true by default.

    For more information, see Configuring JDBC Options.

Default: HintValues.PERSISTENCE_UNIT_DEFAULT

Example: JPA Query API

import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
query.setHint(QueryHints.BIND_PARAMETERS, HintValues.TRUE);

Example: @QueryHint

import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.TargetDatabase;
@QueryHint(name=QueryHints.BIND_PARAMETERS, value=HintValues.TRUE);

Fetch Size

The eclipselink.jdbc.fetch-size hint specifies the number of rows that should be fetched from the database when more rows are needed1

For large queries that return a large number of objects you can configure the row fetch size used in the query to improve performance by reducing the number database hits required to satisfy the selection criteria. Most JDBC drivers default to a fetch size of 10, so if you are reading 1000 objects, increasing the fetch size to 256 can significantly reduce the time required to fetch the query's results. The optimal fetch size is not always obvious. Usually, a fetch size of one half or one quarter of the total expected result size is optimal. Note that if you are unsure of the result set size, incorrectly setting a fetch size too large or too small can decrease performance.

A value of 0 means the JDBC driver default will be used.

Valid values: 0 to Integer.MAX_VALUE (depending on your JDBC driver) as a String.

Default: 0

Note: this value indicates that the JDBC driver default will be used.


1 This property is dependent on the JDBC driver support.

Timeout

The eclipselink.jdbc.timeout hint specifies the number of seconds EclipseLink will wait on a query before throwing a DatabaseException1

A value of 0 means EclipseLink will never time-out a query.

Valid values: 0 to Integer.MAX_VALUE (depending on your JDBC driver) as a String.

Default: 0


1 This property is dependent on the JDBC driver support.

Pessimistic Lock

The eclipselink.pessimistic-lock hint controls whether or not pessimistic locking is used.

The following are the valid values for the org.eclipse.persistence.config.PessimisticLock:

  • NoLock – pessimistic locking is not used.
  • Lock – EclipseLink issues a SELECT .... FOR UPDATE.
  • LockNoWait – EclipseLink issues a SELECT .... FOR UPDATE NO WAIT.

Default: NoLock

Example: JPA Query API

import org.eclipse.persistence.config.PessimisticLock;
import org.eclipse.persistence.config.QueryHints;
query.setHint(QueryHints.PESSIMISTIC_LOCK, PessimisticLock.LockNoWait);

Example: @QueryHint

import org.eclipse.persistence.config.PessimisticLock;
import org.eclipse.persistence.config.QueryHints;
@QueryHint(name=QueryHints.PESSIMISTIC_LOCK, value=PessimisticLock.LockNoWait);

Batch

The eclipselink.batch hint supplies EclipseLink with batching information so subsequent queries of related objects can be optimized in batches instead of being retrieved one-by-one or in one large joined read. Batch reading is more efficient than joining because it avoids reading duplicate data.

Batching is only allowed on queries that have a single object in their select clause.

Valid values: a single-valued relationship path expression.

Note: use dot notation to access nested attributes. For example, to batch-read an employee's manager's address, specify e.manager.address


Example: JPA Query API

import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
query.setHint("eclipselink.batch", "e.address");

Example: @QueryHint

import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
@QueryHint(name=QueryHints.BATCH, value="e.address");

Join Fetch

The eclipselink.join-fetch hint allows joining of the attributes.

This is similar to eclipselink.batch: subsequent queries of related objects can be optimized in batches instead of being retrieved in one large joined read.

This is different from JP QL joining because it allows multilevel fetch joins.

For more information, see Section 4.4.5.3 "Fetch Joins" of JPA specification.

Valid values: a relationship path expression.

Note: use dot notation to access nested attributes.


Example: JPA Query API

import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
query.setHint("eclipselink.join-fetch", "e.address");

Example: @QueryHint

import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
@QueryHint(name=QueryHints.FETCH, value="e.address");

Refresh

The eclipselink.refresh hint controls whether or not to update the EclipseLink session cache with objects that the query returns.

The following are the valid values for the org.eclipse.persistence.config.HintValues:

  • TRUE – refresh cache.
  • FALSE – do not refresh cache.

Default: FALSE

Example: JPA Query API

import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
query.setHint(QueryHints.REFRESH, HintValues.TRUE);

Example: @QueryHint

import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
@QueryHint(name=QueryHints.REFRESH, value=HintValues.TRUE);

Maintain Cache

The eclipselink.maintain-cache hint controls whether or not query results are cached in the session cache and provides a way to query the current database contents without affecting the current persistence context. It configures the query to return unmanaged instances so any updates to entities queried using this hint would have to be merged into the persistence context.

The following are the valid values for the org.eclipse.persistence.config.HintValues:

  • TRUE – maintain cache.
  • FALSE – do not maintain cache.

Default: FALSE

Example: JPA Query API

import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
query.setHint(QueryHints.MAINTAIN_CACHE, HintValues.FALSE);

Example: @QueryHint

import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
@QueryHint(name=QueryHints.MAINTAIN_CACHE, value=HintValues.FALSE);

Read Only

The eclipselink.read-only hint retrieves read-only results back from the query: on nontransactional read operations, where the requested entity types are stored in the shared cache, you can request that the shared instance be returned instead of a detached copy.

Note: you should never modify objects returned from the shared cache.

The following are the valid values for the org.eclipse.persistence.config.HintValues:

  • TRUE – retrieve read-only results back from the query;
  • FALSE – do not retrieve read-only results back from the query.

Default: FALSE

Example: JPA Query API

import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
query.setHint(QueryHints.READ_ONLY, HintValues.TRUE);

Example: @QueryHint

import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
@QueryHint(name=QueryHints.READ_ONLY, value=HintValues.TRUE);

Result Collection Type

The eclipselink.result-collection-type hint configures the concrete class that EclipseLink should use to return its query result.

This lets you specify the type of collection in which the result will be returned.

Valid values: Java Class that implements the Collection interface.

Note: typically, you would execute these queries by calling the getResultsList method, which returns the java.util.List, on the Query. This means that the class specified in this hint must implement the List interface, if you are invoking it using the getResultsList method.

Note: specify the class without the ".class" notation. For example, java.util.Vector would work, not java.util.Vector.classEclipseLink will throw an exception, if you use this hint with a class that does not implement the Collection interface.

Default:java.util.Vector

Example: JPA Query API

import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
query.setHint("eclipselink.result-collection-type", java.util.ArrayList.class);

Example: @QueryHint

import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
@QueryHint(name=QueryHints.RESULT_COLLECTION_TYPE, value="java.util.ArrayList");


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

Back to the top