TypedQuery
- Type Parameters:
<X>- query result type
- Super Interfaces:
Query
Interface used to control the execution of typed queries.
- Since:
- JPA 2.0
Chapter 4 - JPA Queries (JPQL / Criteria) explains how to use
TypedQuery.Public Methods
int executeUpdate()
- Return:
- the number of entities updated or deleted
- Throws:
- IllegalStateException - if called for a Jakarta Persistence query language SELECT statement or for a criteria query
- TransactionRequiredException - if there is no transaction or the persistence context has not been joined to the transaction
- QueryTimeoutException - if the statement execution exceeds the query timeout value set and only the statement is rolled back
- PersistenceException - if the query execution exceeds the query timeout value set and the transaction is rolled back
- TransactionRequiredException - if there is no transaction or the persistence context has not been joined to the transaction
- Inherited from:
Query
- Since:
- JPA 1.0
- Inherited from:
Query
- Since:
- JPA 3.2
- Inherited from:
Query
- Since:
- JPA 3.2
int getFirstResult()
Returns
0 if setFirstResult was not applied to the query object. - Return:
- position of the first result
- Inherited from:
Query
- Since:
- 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.
- Return:
- flush mode
- Inherited from:
Query
- Since:
- JPA 2.0
Returns null if a lock mode has not been set on the query object.
- Return:
- lock mode
- Throws:
- IllegalStateException - if the query is found not to be a Jakarta Persistence query language SELECT query or a {@link jakarta.persistence.criteria.CriteriaQuery} query
- Inherited from:
Query
- Since:
- JPA 2.0
int getMaxResults()
Returns Integer if setMaxResults() was not applied to the query object.
- Return:
- maximum number of results
- Inherited from:
Query
- Since:
- JPA 2.0
This method is not required to be supported for native queries.
- Parameters:
name- parameter name
- Return:
- parameter object
- Throws:
- IllegalArgumentException - if the parameter of the specified name does not exist
- IllegalStateException - if invoked on a native query when the implementation does not support this use
- Inherited from:
Query
- Since:
- JPA 2.0
This method is required to be supported for criteria queries only.
- Parameters:
name- parameter nametype- type
- Return:
- parameter object
- Throws:
- IllegalArgumentException - if the parameter of the specified name does not exist or is not assignable to the type
- IllegalStateException - if invoked on a native query or Jakarta Persistence query language query when the implementation does not support this use
- Inherited from:
Query
- Since:
- JPA 2.0
This method is not required to be supported for native queries.
- Parameters:
position- position
- Return:
- parameter object
- Throws:
- IllegalArgumentException - if the parameter with the specified position does not exist
- IllegalStateException - if invoked on a native query when the implementation does not support this use
- Inherited from:
Query
- Since:
- JPA 2.0
This method is not required to be supported by the provider.
- Parameters:
position- positiontype- type
- Return:
- parameter object
- Throws:
- IllegalArgumentException - if the parameter with the specified position does not exist or is not assignable to the type
- IllegalStateException - if invoked on a native query or Jakarta Persistence query language query when the implementation does not support this use
- Inherited from:
Query
- Since:
- JPA 2.0
T getParameterValue(Parameter<T> param)
(Note that OUT parameters are unbound.)
- Parameters:
param- parameter object
- Return:
- parameter value
- Throws:
- IllegalArgumentException - if the parameter is not a parameter of the query
- IllegalStateException - if the parameter has not been bound
- Inherited from:
Query
- Since:
- JPA 2.0
Object getParameterValue(String name)
(Note that OUT parameters are unbound.)
- Parameters:
name- parameter name
- Return:
- parameter value
- Throws:
- IllegalStateException - if the parameter has not been bound
- IllegalArgumentException - if the parameter of the specified name does not exist
- Inherited from:
Query
- Since:
- JPA 2.0
Object getParameterValue(int position)
(Note that OUT parameters are unbound.)
- Parameters:
position- position
- Return:
- parameter value
- Throws:
- IllegalStateException - if the parameter has not been bound
- IllegalArgumentException - if the parameter with the specified position does not exist
- Inherited from:
Query
- Since:
- JPA 2.0
Returns empty set if the query has no parameters. This method is not required to be supported for native queries.
- Return:
- set of the parameter objects
- Throws:
- IllegalStateException - if invoked on a native query when the implementation does not support this use
- Inherited from:
Query
- Since:
- JPA 2.0
List<X> getResultList()
- Return:
- a list of the results, each of type {@link X} , or an empty list if there are no results
- Throws:
- IllegalStateException - if called for a Jakarta Persistence query language UPDATE or DELETE statement
- QueryTimeoutException - if the query execution exceeds the query timeout value set and only the statement is rolled back
- TransactionRequiredException - if a lock mode other than {@code NONE} has been set and there is no transaction or the persistence context has not been joined to the transaction
- PessimisticLockException - if pessimistic locking fails and the transaction is rolled back
- LockTimeoutException - if pessimistic locking fails and only the statement is rolled back
- PersistenceException - if the query execution exceeds the query timeout value set and the transaction is rolled back
- QueryTimeoutException - if the query execution exceeds the query timeout value set and only the statement is rolled back
- Since:
- JPA 2.0
By default, this method delegates to List, however, persistence provider may choose to override this method to provide additional capabilities.
- Return:
- a stream of the results, each of type {@link X} , or an empty stream if there are no results
- Throws:
- IllegalStateException - if called for a Jakarta Persistence query language UPDATE or DELETE statement
- QueryTimeoutException - if the query execution exceeds the query timeout value set and only the statement is rolled back
- TransactionRequiredException - if a lock mode other than {@code NONE} has been set and there is no transaction or the persistence context has not been joined to the transaction
- PessimisticLockException - if pessimistic locking fails and the transaction is rolled back
- LockTimeoutException - if pessimistic locking fails and only the statement is rolled back
- PersistenceException - if the query execution exceeds the query timeout value set and the transaction is rolled back
- QueryTimeoutException - if the query execution exceeds the query timeout value set and only the statement is rolled back
- See Also:
- Stream
- getResultList()
- Since:
- JPA 2.2
X getSingleResult()
- Return:
- the result, of type {@link X}
- Throws:
- NoResultException - if there is no result
- NonUniqueResultException - if more than one result
- IllegalStateException - if called for a Jakarta Persistence query language UPDATE or DELETE statement
- QueryTimeoutException - if the query execution exceeds the query timeout value set and only the statement is rolled back
- TransactionRequiredException - if a lock mode other than {@code NONE} has been set and there is no transaction or the persistence context has not been joined to the transaction
- PessimisticLockException - if pessimistic locking fails and the transaction is rolled back
- LockTimeoutException - if pessimistic locking fails and only the statement is rolled back
- PersistenceException - if the query execution exceeds the query timeout value set and the transaction is rolled back
- NonUniqueResultException - if more than one result
- Since:
- JPA 2.0
- Return:
- the result, of type {@link X} , or null if there is no result
- Throws:
- NonUniqueResultException - if more than one result
- IllegalStateException - if called for a Jakarta Persistence query language UPDATE or DELETE statement
- QueryTimeoutException - if the query execution exceeds the query timeout value set and only the statement is rolled back
- TransactionRequiredException - if a lock mode other than {@code NONE} has been set and there is no transaction or the persistence context has not been joined to the transaction
- PessimisticLockException - if pessimistic locking fails and the transaction is rolled back
- LockTimeoutException - if pessimistic locking fails and only the statement is rolled back
- PersistenceException - if the query execution exceeds the query timeout value set and the transaction is rolled back
- IllegalStateException - if called for a Jakarta Persistence query language UPDATE or DELETE statement
- Since:
- JPA 3.2
Integer getTimeout()
- Inherited from:
Query
- Since:
- JPA 3.2
- Parameters:
param- parameter object
- Return:
- boolean indicating whether parameter has been bound
- Inherited from:
Query
- Since:
- JPA 2.0
This cache retrieval mode overrides the cache retrieve mode in use by the entity manager.
- Parameters:
cacheRetrieveMode- cache retrieval mode
- Return:
- the same query instance
- Since:
- JPA 3.2
This cache storage mode overrides the cache storage mode in use by the entity manager.
- Parameters:
cacheStoreMode- cache storage mode
- Return:
- the same query instance
- Since:
- JPA 3.2
- Parameters:
startPosition- position of the first result, numbered from 0
- Return:
- the same query instance
- Throws:
- IllegalArgumentException - if the argument is negative
- Since:
- JPA 2.0
The flush mode type applies to the query regardless of the flush mode type in use for the entity manager.
- Parameters:
flushMode- flush mode
- Return:
- the same query instance
- Since:
- JPA 2.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.
- Parameters:
hintName- name of property or hintvalue- value for the property or hint
- Return:
- the same query instance
- Throws:
- IllegalArgumentException - if the second argument is not valid for the implementation
- Since:
- JPA 2.0
- Parameters:
lockMode- lock mode
- Return:
- the same query instance
- Throws:
- IllegalStateException - if the query is found not to be a Jakarta Persistence query language SELECT query or a {@link jakarta.persistence.criteria.CriteriaQuery} query
- Since:
- JPA 2.0
- Parameters:
maxResult- maximum number of results to retrieve
- Return:
- the same query instance
- Throws:
- IllegalArgumentException - if the argument is negative
- Since:
- JPA 2.0
- Parameters:
param- parameter objectvalue- parameter value
- Return:
- the same query instance
- Throws:
- IllegalArgumentException - if the parameter does not correspond to a parameter of the query
- Since:
- JPA 2.0
- Parameters:
name- parameter namevalue- parameter value
- Return:
- the same query instance
- Throws:
- IllegalArgumentException - if the parameter name does not correspond to a parameter of the query or if the argument is of incorrect type
- Since:
- JPA 2.0
- Parameters:
position- positionvalue- parameter value
- Return:
- the same query instance
- Throws:
- IllegalArgumentException - if position does not correspond to a positional parameter of the query or if the argument is of incorrect type
- Since:
- JPA 2.0
This is a hint, and is an alternative to plain
jakarta.persistence.query.timeout. - Parameters:
timeout- the timeout, in milliseconds, or null to indicate no timeout
- Return:
- the same query instance
- Since:
- JPA 3.2
T unwrap(Class<T> cls)
If the provider implementation of
Query does not support the given type, the PersistenceException is thrown. - Parameters:
cls- the type of the object to be returned. This is usually either the underlying class implementing {@code Query} or an interface it implements.
- Return:
- an instance of the specified class
- Throws:
- PersistenceException - if the provider does not support the given type
- Inherited from:
Query
- Since:
- JPA 2.0