Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/UserGuide/JPA/Basic JPA Development/Querying/Query Hints

Warning See Java Persistence API (JPA) Extensions Reference for EclipseLink, EclipseLink 2.4


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


EclipseLink JPA Query Hints

You can use a query hint to customize or optimize a JPA query. All EclipseLink query hints are defined in the QueryHints class in the org.eclipse.persistence.config package.

Elug javaspec icon.gif

For more information, see Section 10.3.1 "NamedQuery Annotation" in the JPA Specification.

Use EclipseLink JPA query hints to:

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 following 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.

Valid values for org.eclipse.persistence.config.CacheUsage
Value Description
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.
UseEntityDefault Use the cache configuration as specified by the EclipseLink descriptor API for this entity.

Default

Elug note icon.png

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.


Example: JPA Query API

 import org.eclipse.persistence.config.CacheUsage;
 import org.eclipse.persistence.config.QueryHints;
 query.setHint(QueryHints.CACHE_USAGE, CacheUsage.CheckCacheOnly);

Example: @QueryHint

 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.

Valid values for the org.eclipse.persistence.config.QueryType
Value Description
Auto EclipseLink chooses the type of query to use.
Default
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.


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.

Valid values for the org.eclipse.persistence.config.HintValues
Value Description
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. 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.
1 This property is dependent on the JDBC driver support.

Value Description
0 to Integer.MAX_VALUE As a String, depending on your JDBC driver. A value of 0 means the JDBC driver default will be used.
Default = 0.

Timeout

The eclipselink.jdbc.timeout hint specifies the number of seconds EclipseLink will wait on a query before throwing a DatabaseException1
1 This property is dependent on the JDBC driver support.

Value Description
0 to Integer.MAX_VALUE As a String, depending on your JDBC driver. A value of 0 means EclipseLink will never time-out a query.
Default = 0.

Pessimistic Lock

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

Valid values for the org.eclipse.persistence.config.PessimisticLock
Value Description
NoLock Pessimistic locking is not used.
Default
Lock EclipseLink issues a SELECT .... FOR UPDATE.
LockNoWait EclipseLink issues a SELECT .... FOR UPDATE NO WAIT.

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.

Elug note icon.png

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 [[#Batch|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.
Elug javaspec icon.gif

For more information, see Section 4.4.5.3 "Fetch Joins" in the JPA Specification.

Valid values: a relationship path expression.

Elug note icon.png

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.

Valid values for the org.eclipse.persistence.config.HintValues
Value Description
TRUE Refresh cache.
FALSE Do not refresh cache.
Default


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.

Valid values for the org.eclipse.persistence.config.HintValues
Value Description
TRUE Maintain cache.
FALSE Do not maintain cache.
Default

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.

Elug note icon.png

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

Valid values for the org.eclipse.persistence.config.HintValues
Value Description
TRUE Retrieve read-only results back from the query
FALSE Do not retrieve read-only results back from the query.
Default

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.

Elug note icon.png

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.

Elug note icon.png

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: 2.2.0 DRAFT
Other versions...

Back to the top