Jakarta Persistence (JPA) Interface
jakarta.persistence.TypedQuery<X>
- Type Parameters:
<X>- query result type
- Super Interfaces:
Query
Interface used to control the execution of typed queries.
- See Also:
- Since:
- Jakarta Persistence (JPA) 2.0
Chapter 4 - JPA Queries (JPQL / Criteria) explains how to use
TypedQuery.Public Instance Methods
int executeUpdate()Inherited from
Query- Returns:
- the number of entities updated or deleted.
- Throws:
- if called for a Jakarta Persistence query language SELECT statement or for a criteria query.IllegalStateException- if the query execution exceeds the query timeout value set and the transaction is rolled back.PersistenceException- if there is no transaction or the persistence context has not been joined to the transaction.TransactionRequiredException- if the statement execution exceeds the query timeout value set and only the statement is rolled back.QueryTimeoutException
- Since:
- Jakarta Persistence (JPA) 1.0
Inherited from
Query- Since:
- Jakarta Persistence (JPA) 3.2
Inherited from
Query- Since:
- Jakarta Persistence (JPA) 3.2
int getFirstResult()Returns
0 if setFirstResult was not applied to the query object.Inherited from
Query- Returns:
- position of the first result.
- Since:
- Jakarta Persistence (JPA) 2.0
The Running JPA Queries article explains how to use
getFirstResult.If a flush mode has not been set for the query object, returns the flush mode in effect for the entity manager.
Inherited from
Query- Returns:
- flush mode.
- Since:
- Jakarta Persistence (JPA) 2.0
Map<String,Object> getHints()Returns null if a lock mode has not been set on the query object.
Inherited from
Query- Returns:
- lock mode.
- Throws:
- if the query is found not to be a Jakarta Persistence query language SELECT query or aIllegalStateExceptionCriteriaQuery<T>query.
- Since:
- Jakarta Persistence (JPA) 2.0
int getMaxResults()Returns
Integer.java.lang.Integer/MAX_VALUE if Query.setMaxResults was not applied to the query object.Inherited from
Query- Returns:
- maximum number of results.
- Since:
- Jakarta Persistence (JPA) 2.0
This method is not required to be supported for native queries.
Inherited from
Query- Parameters:
name- parameter name
- Returns:
- parameter object.
- Throws:
- if invoked on a native query when the implementation does not support this use.IllegalStateException- if the parameter of the specified name does not exist.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.0
This method is required to be supported for criteria queries only.
Inherited from
Query- Parameters:
name- parameter nametype- type
- Returns:
- parameter object.
- Throws:
- if invoked on a native query or Jakarta Persistence query language query when the implementation does not support this use.IllegalStateException- if the parameter of the specified name does not exist or is not assignable to the type.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.0
This method is not required to be supported for native queries.
Inherited from
Query- Parameters:
position- position
- Returns:
- parameter object.
- Throws:
- if invoked on a native query when the implementation does not support this use.IllegalStateException- if the parameter with the specified position does not exist.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.0
This method is not required to be supported by the provider.
Inherited from
Query- Parameters:
position- positiontype- type
- Returns:
- parameter object.
- Throws:
- if invoked on a native query or Jakarta Persistence query language query when the implementation does not support this use.IllegalStateException- if the parameter with the specified position does not exist or is not assignable to the type.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.0
(Note that OUT parameters are unbound.)
Inherited from
Query- Parameters:
param- parameter object
- Returns:
- parameter value.
- Throws:
- if the parameter has not been bound.IllegalStateException- if the parameter is not a parameter of the query.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.0
(Note that OUT parameters are unbound.)
Inherited from
Query- Parameters:
name- parameter name
- Returns:
- parameter value.
- Throws:
- if the parameter has not been bound.IllegalStateException- if the parameter of the specified name does not exist.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.0
(Note that OUT parameters are unbound.)
Inherited from
Query- Parameters:
position- position
- Returns:
- parameter value.
- Throws:
- if the parameter has not been bound.IllegalStateException- if the parameter with the specified position does not exist.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 2.0
Returns empty set if the query has no parameters. This method is not required to be supported for native queries.
Inherited from
Query- Returns:
- set of the parameter objects.
- Throws:
- if invoked on a native query when the implementation does not support this use.IllegalStateException
- Since:
- Jakarta Persistence (JPA) 2.0
List<X> getResultList()Overrides
Query.getResultList- Returns:
- a list of the results, each of type
X, or an empty list if there are no results.
- Throws:
- if called for a Jakarta Persistence query language UPDATE or DELETE statement.IllegalStateException- if pessimistic locking fails and the transaction is rolled back.PessimisticLockException- if pessimistic locking fails and only the statement is rolled back.LockTimeoutException- if the query execution exceeds the query timeout value set and the transaction is rolled back.PersistenceException- if a lock mode other thanTransactionRequiredExceptionNONEhas been set and there is no transaction or the persistence context has not been joined to the transaction.- if the query execution exceeds the query timeout value set and only the statement is rolled back.QueryTimeoutException
- Since:
- Jakarta Persistence (JPA) 1.0
Stream<X> getResultStream()By default, this method delegates to getResultList().stream(), however, persistence provider may choose to override this method to provide additional capabilities.
Overrides
Query.getResultStream- Returns:
- a stream of the results, each of type
X, or an empty stream if there are no results.
- Throws:
- if called for a Jakarta Persistence query language UPDATE or DELETE statement.IllegalStateException- if pessimistic locking fails and the transaction is rolled back.PessimisticLockException- if pessimistic locking fails and only the statement is rolled back.LockTimeoutException- if the query execution exceeds the query timeout value set and the transaction is rolled back.PersistenceException- if a lock mode other thanTransactionRequiredExceptionNONEhas been set and there is no transaction or the persistence context has not been joined to the transaction.- if the query execution exceeds the query timeout value set and only the statement is rolled back.QueryTimeoutException
- See Also:
- Since:
- Jakarta Persistence (JPA) 2.2
X getSingleResult()Overrides
Query.getSingleResult- Returns:
- the result, of type
X.
- Throws:
- if called for a Jakarta Persistence query language UPDATE or DELETE statement.IllegalStateException- if pessimistic locking fails and only the statement is rolled back.LockTimeoutException- if more than one result.NonUniqueResultException- if the query execution exceeds the query timeout value set and only the statement is rolled back.QueryTimeoutException- if pessimistic locking fails and the transaction is rolled back.PessimisticLockException- if there is no result.NoResultException- if the query execution exceeds the query timeout value set and the transaction is rolled back.PersistenceException- if a lock mode other thanTransactionRequiredExceptionNONEhas been set and there is no transaction or the persistence context has not been joined to the transaction.
- Since:
- Jakarta Persistence (JPA) 1.0
Overrides
Query.getSingleResultOrNull- Returns:
- the result, of type
X, or null if there is no result.
- Throws:
- if called for a Jakarta Persistence query language UPDATE or DELETE statement.IllegalStateException- if pessimistic locking fails and only the statement is rolled back.LockTimeoutException- if more than one result.NonUniqueResultException- if the query execution exceeds the query timeout value set and only the statement is rolled back.QueryTimeoutException- if pessimistic locking fails and the transaction is rolled back.PessimisticLockException- if the query execution exceeds the query timeout value set and the transaction is rolled back.PersistenceException- if a lock mode other thanTransactionRequiredExceptionNONEhas been set and there is no transaction or the persistence context has not been joined to the transaction.
- Since:
- Jakarta Persistence (JPA) 3.2
Integer getTimeout()Inherited from
Query- Since:
- Jakarta Persistence (JPA) 3.2
Inherited from
Query- Parameters:
param- parameter object
- Returns:
- boolean indicating whether parameter has been bound.
- Since:
- Jakarta Persistence (JPA) 2.0
This cache retrieval mode overrides the cache retrieve mode in use by the entity manager.
Overrides
Query.setCacheRetrieveMode- Parameters:
cacheRetrieveMode- cache retrieval mode
- Returns:
- the same query instance.
- Since:
- Jakarta Persistence (JPA) 3.2
This cache storage mode overrides the cache storage mode in use by the entity manager.
Overrides
Query.setCacheStoreMode- Parameters:
cacheStoreMode- cache storage mode
- Returns:
- the same query instance.
- Since:
- Jakarta Persistence (JPA) 3.2
Overrides
Query.setFirstResult- Parameters:
startPosition- position of the first result, numbered from 0
- Returns:
- the same query instance.
- Throws:
- if the argument is negative.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 1.0
The flush mode type applies to the query regardless of the flush mode type in use for the entity manager.
Overrides
Query.setFlushMode- Parameters:
flushMode- flush mode
- Returns:
- the same query instance.
- Since:
- Jakarta Persistence (JPA) 1.0
The hints elements may be used to specify query properties and hints. Properties defined by this specification must be observed by the provider. Vendor-specific hints that are not recognized by a provider must be silently ignored. Portable applications should not rely on the standard timeout hint. Depending on the database in use and the locking mechanisms used by the provider, this hint may or may not be observed.
Overrides
Query.setHint- Parameters:
value- value for the property or hinthintName- name of property or hint
- Returns:
- the same query instance.
- Throws:
- if the second argument is not valid for the implementation.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 1.0
Overrides
Query.setLockMode- Parameters:
lockMode- lock mode
- Returns:
- the same query instance.
- Throws:
- if the query is found not to be a Jakarta Persistence query language SELECT query or aIllegalStateExceptionCriteriaQuery<T>query.
- Since:
- Jakarta Persistence (JPA) 1.0
Overrides
Query.setMaxResults- Parameters:
maxResult- maximum number of results to retrieve
- Returns:
- the same query instance.
- Throws:
- if the argument is negative.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 1.0
Overrides
Query.setParameter- Parameters:
param- parameter objectvalue- parameter value
- Returns:
- the same query instance.
- Throws:
- if the parameter does not correspond to a parameter of the query.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 1.0
Overrides
Query.setParameter- Parameters:
temporalType- temporal typeparam- parameter objectvalue- parameter value
- Returns:
- the same query instance.
- Throws:
- if the parameter does not correspond to a parameter of the query.IllegalArgumentException
- Deprecated:
- Newly-written code should use the date/time types defined in
java.time.
- Since:
- Jakarta Persistence (JPA) 1.0
Overrides
Query.setParameter- Parameters:
temporalType- temporal typeparam- parameter objectvalue- parameter value
- Returns:
- the same query instance.
- Throws:
- if the parameter does not correspond to a parameter of the query.IllegalArgumentException
- Deprecated:
- Newly-written code should use the date/time types defined in
java.time.
- Since:
- Jakarta Persistence (JPA) 1.0
Overrides
Query.setParameter- Parameters:
name- parameter namevalue- parameter value
- Returns:
- the same query instance.
- Throws:
- if the parameter name does not correspond to a parameter of the query or if the argument is of incorrect type.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 1.0
Overrides
Query.setParameter- Parameters:
name- parameter nametemporalType- temporal typevalue- parameter value
- Returns:
- the same query instance.
- Throws:
- if the parameter name does not correspond to a parameter of the query or if the value argument is of incorrect type.IllegalArgumentException
- Deprecated:
- Newly-written code should use the date/time types defined in
java.time.
- Since:
- Jakarta Persistence (JPA) 1.0
Overrides
Query.setParameter- Parameters:
name- parameter nametemporalType- temporal typevalue- parameter value
- Returns:
- the same query instance.
- Throws:
- if the parameter name does not correspond to a parameter of the query or if the value argument is of incorrect type.IllegalArgumentException
- Deprecated:
- Newly-written code should use the date/time types defined in
java.time.
- Since:
- Jakarta Persistence (JPA) 1.0
Overrides
Query.setParameter- Parameters:
position- positionvalue- parameter value
- Returns:
- the same query instance.
- Throws:
- if position does not correspond to a positional parameter of the query or if the argument is of incorrect type.IllegalArgumentException
- Since:
- Jakarta Persistence (JPA) 1.0
Overrides
Query.setParameter- Parameters:
position- positiontemporalType- temporal typevalue- parameter value
- Returns:
- the same query instance.
- Throws:
- if position does not correspond to a positional parameter of the query or if the value argument is of incorrect type.IllegalArgumentException
- Deprecated:
- Newly-written code should use the date/time types defined in
java.time.
- Since:
- Jakarta Persistence (JPA) 1.0
Overrides
Query.setParameter- Parameters:
position- positiontemporalType- temporal typevalue- parameter value
- Returns:
- the same query instance.
- Throws:
- if position does not correspond to a positional parameter of the query or if the value argument is of incorrect type.IllegalArgumentException
- Deprecated:
- Newly-written code should use the date/time types defined in
java.time.
- Since:
- Jakarta Persistence (JPA) 1.0
This is a hint, and is an alternative to setting the hint
jakarta.persistence.query.timeout.Overrides
Query.setTimeout- Parameters:
timeout- the timeout, in milliseconds, or null to indicate no timeout
- Returns:
- the same query instance.
- Since:
- Jakarta Persistence (JPA) 3.2
If the provider implementation of
Query does not support the given type, the PersistenceException is thrown.Inherited from
Query- Parameters:
cls- the type of the object to be returned. This is usually either the underlying class implementingQueryor an interface it implements.
- Returns:
- an instance of the specified class.
- Throws:
- if the provider does not support the given type.PersistenceException
- Since:
- Jakarta Persistence (JPA) 2.0