JPA Interface

TypedQuery

Type Parameters:
<X> - query result type
Super Interfaces:
Query

Interface used to control the execution of typed queries.
See Also:
Query
Parameter
Since:
JPA 2.0
Chapter 4 - JPA Queries (JPQL / Criteria) explains how to use TypedQuery.

Public Methods

Execute an update or delete statement.
Return:
the number of entities updated or deleted
Throws:
IllegalStateException - if called for a Java 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
Inherited from:
Query
Since:
JPA 1.0
The position of the first result the query object was set to retrieve.
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.
Get the flush mode in effect for the query execution.
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
Map<String,Object> getHints()
Get the properties and hints and associated values that are in effect for the query instance.
Return:
query properties and hints
Inherited from:
Query
Since:
JPA 2.0
Get the current lock mode for the query.
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 Java Persistence query language SELECT query or a Criteria API query
Inherited from:
Query
Since:
JPA 2.0
The maximum number of results the query object was set to retrieve.
Returns Integer.MAX_VALUE if setMaxResults was not applied to the query object.
Return:
maximum number of results
Inherited from:
Query
Since:
JPA 2.0
Parameter<?> getParameter(String name)
Get the parameter object corresponding to the declared parameter of the given name.
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
Parameter<T> getParameter(String name, Class<T> type)
Get the parameter object corresponding to the declared parameter of the given name and type.
This method is required to be supported for criteria queries only.
Parameters:
name - parameter name
type - 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 Java Persistence query language query when the implementation does not support this use
Inherited from:
Query
Since:
JPA 2.0
Parameter<?> getParameter(int position)
Get the parameter object corresponding to the declared positional parameter with the given position.
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
Parameter<T> getParameter(int position, Class<T> type)
Get the parameter object corresponding to the declared positional parameter with the given position and type.
This method is not required to be supported by the provider.
Parameters:
position - position
type - 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 Java Persistence query language query when the implementation does not support this use
Inherited from:
Query
Since:
JPA 2.0
Return the input value bound to the parameter.
(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 been bound
Inherited from:
Query
Since:
JPA 2.0
Object getParameterValue(String name)
Return the input value bound to the named parameter.
(Note that OUT parameters are unbound.)
Parameters:
name - parameter name
Return:
parameter value
Throws:
IllegalStateException - if the parameter has not been been bound
IllegalArgumentException - if the parameter of the specified name does not exist
Inherited from:
Query
Since:
JPA 2.0
Object getParameterValue(int position)
Return the input value bound to the positional parameter.
(Note that OUT parameters are unbound.)
Parameters:
position - position
Return:
parameter value
Throws:
IllegalStateException - if the parameter has not been been bound
IllegalArgumentException - if the parameter with the specified position does not exist
Inherited from:
Query
Since:
JPA 2.0
Get the parameter objects corresponding to the declared parameters of the query.
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()
Execute a SELECT query and return the query results as a typed List.
Return:
a list of the results
Throws:
IllegalStateException - if called for a Java 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 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
Since:
JPA 2.0
Execute a SELECT query and return the query results as a typed java.util.stream.Stream.
By default this method delegates to getResultList().stream(), however persistence provider may choose to override this method to provide additional capabilities.
Return:
a stream of the results
Throws:
IllegalStateException - if called for a Java 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 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
See Also:
Stream
getResultList()
Since:
JPA 2.2
Execute a SELECT query that returns a single result.
Return:
the result
Throws:
NoResultException - if there is no result
NonUniqueResultException - if more than one result
IllegalStateException - if called for a Java 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 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
Since:
JPA 2.0
boolean isBound(Parameter<?> param)
Return a boolean indicating whether a value has been bound to the parameter.
Parameters:
param - parameter object
Return:
boolean indicating whether parameter has been bound
Inherited from:
Query
Since:
JPA 2.0
TypedQuery<X> setFirstResult(int startPosition)
Set the position of the first result to retrieve.
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
Set the flush mode type to be used for the query execution.
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
TypedQuery<X> setHint(String hintName, Object value)
Set a query property or hint.
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 hint
value - 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
Set the lock mode type to be used for the query execution.
Parameters:
lockMode - lock mode
Return:
the same query instance
Throws:
IllegalStateException - if the query is found not to be a Java Persistence query language SELECT query or a CriteriaQuery query
Since:
JPA 2.0
TypedQuery<X> setMaxResults(int maxResult)
Set the maximum number of results to retrieve.
Parameters:
maxResult - maximum number of results to retrieve
Return:
the same query instance
Throws:
IllegalArgumentException - if the argument is negative
Since:
JPA 2.0
TypedQuery<X> setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType)
Bind an instance of java.util.Calendar to a Parameter object.
Parameters:
param - parameter object
value - parameter value
temporalType - temporal type
Return:
the same query instance
Throws:
IllegalArgumentException - if the parameter does not correspond to a parameter of the query
Since:
JPA 2.0
TypedQuery<X> setParameter(Parameter<Date> param, Date value, TemporalType temporalType)
Bind an instance of java.util.Date to a Parameter object.
Parameters:
param - parameter object
value - parameter value
temporalType - temporal type
Return:
the same query instance
Throws:
IllegalArgumentException - if the parameter does not correspond to a parameter of the query
Since:
JPA 2.0
TypedQuery<X> setParameter(Parameter<T> param, T value)
Bind the value of a Parameter object.
Parameters:
param - parameter object
value - 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
TypedQuery<X> setParameter(String name, Calendar value, TemporalType temporalType)
Bind an instance of java.util.Calendar to a named parameter.
Parameters:
name - parameter name
value - parameter value
temporalType - temporal type
Return:
the same query instance
Throws:
IllegalArgumentException - if the parameter name does not correspond to a parameter of the query or if the value argument is of incorrect type
Since:
JPA 2.0
TypedQuery<X> setParameter(String name, Date value, TemporalType temporalType)
Bind an instance of java.util.Date to a named parameter.
Parameters:
name - parameter name
value - parameter value
temporalType - temporal type
Return:
the same query instance
Throws:
IllegalArgumentException - if the parameter name does not correspond to a parameter of the query or if the value argument is of incorrect type
Since:
JPA 2.0
TypedQuery<X> setParameter(String name, Object value)
Bind an argument value to a named parameter.
Parameters:
name - parameter name
value - 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
TypedQuery<X> setParameter(int position, Calendar value, TemporalType temporalType)
Bind an instance of java.util.Calendar to a positional parameter.
Parameters:
position - position
value - parameter value
temporalType - temporal type
Return:
the same query instance
Throws:
IllegalArgumentException - if position does not correspond to a positional parameter of the query or if the value argument is of incorrect type
Since:
JPA 2.0
TypedQuery<X> setParameter(int position, Date value, TemporalType temporalType)
Bind an instance of java.util.Date to a positional parameter.
Parameters:
position - position
value - parameter value
temporalType - temporal type
Return:
the same query instance
Throws:
IllegalArgumentException - if position does not correspond to a positional parameter of the query or if the value argument is of incorrect type
Since:
JPA 2.0
TypedQuery<X> setParameter(int position, Object value)
Bind an argument value to a positional parameter.
Parameters:
position - position
value - 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
T unwrap(Class<T> cls)
Return an object of the specified type to allow access to the provider-specific API.
If the provider's query implementation does not support the specified class, the PersistenceException is thrown.
Parameters:
cls - the class of the object to be returned. This is normally either the underlying query implementation class or an interface that it implements.
Return:
an instance of the specified class
Throws:
PersistenceException - if the provider does not support the call
Inherited from:
Query
Since:
JPA 2.0