- All Known Subinterfaces:
- StoredProcedureQuery,- TypedQuery<X>
public interface Query
Interface used to control query execution.
- Since:
- 1.0
- See Also:
- 
Method SummaryModifier and TypeMethodDescriptionintExecute an update or delete statement.The cache retrieval mode that will be in effect during query execution.The cache storage mode that will be in effect during query execution.intThe position of the first result the query object was set to retrieve.Get the flush mode in effect for the query execution.getHints()Get the properties and hints and associated values that are in effect for the query instance.Get the current lock mode for the query.intThe maximum number of results the query object was set to retrieve.Parameter<?> getParameter(int position) Get the parameter object corresponding to the declared positional parameter with the given position.<T> Parameter<T> getParameter(int position, Class<T> type) Get the parameter object corresponding to the declared positional parameter with the given position and type.Parameter<?> getParameter(String name) Get the parameter object corresponding to the declared parameter of the given name.<T> Parameter<T> getParameter(String name, Class<T> type) Get the parameter object corresponding to the declared parameter of the given name and type.Get the parameter objects corresponding to the declared parameters of the query.getParameterValue(int position) Return the input value bound to the positional parameter.<T> TgetParameterValue(Parameter<T> param) Return the input value bound to the parameter.getParameterValue(String name) Return the input value bound to the named parameter.Execute a SELECT query and return the query results as an untypedList.default StreamExecute a SELECT query and return the query results as an untypedStream.Execute a SELECT query that returns a single untyped result.Execute a SELECT query that returns a single untyped result.The query timeout.booleanReturn a boolean indicating whether a value has been bound to the parameter.setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode) Set the cache retrieval mode that is in effect during query execution.setCacheStoreMode(CacheStoreMode cacheStoreMode) Set the cache storage mode that is in effect during query execution.setFirstResult(int startPosition) Set the position of the first result to retrieve.setFlushMode(FlushModeType flushMode) Set the flush mode type to be used for the query execution.Set a query property or hint.setLockMode(LockModeType lockMode) Set the lock mode type to be used for the query execution.setMaxResults(int maxResult) Set the maximum number of results to retrieve.setParameter(int position, Object value) Bind an argument value to a positional parameter.setParameter(int position, Calendar value, TemporalType temporalType) Deprecated.setParameter(int position, Date value, TemporalType temporalType) Deprecated.Newly-written code should use the date/time types defined injava.time.setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType) Deprecated.Newly-written code should use the date/time types defined injava.time.setParameter(Parameter<Date> param, Date value, TemporalType temporalType) Deprecated.Newly-written code should use the date/time types defined injava.time.<T> QuerysetParameter(Parameter<T> param, T value) Bind the value of aParameterobject.setParameter(String name, Object value) Bind an argument value to a named parameter.setParameter(String name, Calendar value, TemporalType temporalType) Deprecated.Newly-written code should use the date/time types defined injava.time.setParameter(String name, Date value, TemporalType temporalType) Deprecated.Newly-written code should use the date/time types defined injava.time.setTimeout(Integer timeout) Set the query timeout, in milliseconds.<T> TReturn an object of the specified type to allow access to a provider-specific API.
- 
Method Details- 
getResultListList getResultList()Execute a SELECT query and return the query results as an untypedList.- Returns:
- a list of the results, 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- NONEhas 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
 
- 
getResultStreamExecute a SELECT query and return the query results as an untypedStream.By default, this method delegates to getResultList().stream(), however persistence provider may choose to override this method to provide additional capabilities.- Returns:
- a stream of the results, 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- NONEhas 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:
- 2.2
- See Also:
 
- 
getSingleResultObject getSingleResult()Execute a SELECT query that returns a single untyped result.- Returns:
- the result
- 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- NONEhas 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
 
- 
getSingleResultOrNullObject getSingleResultOrNull()Execute a SELECT query that returns a single untyped result.- Returns:
- the result, 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- NONEhas 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:
- 3.2
 
- 
executeUpdateint executeUpdate()Execute an update or delete statement.- Returns:
- 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
 
- 
setMaxResultsSet the maximum number of results to retrieve.- Parameters:
- maxResult- maximum number of results to retrieve
- Returns:
- the same query instance
- Throws:
- IllegalArgumentException- if the argument is negative
 
- 
getMaxResultsint getMaxResults()The maximum number of results the query object was set to retrieve. ReturnsInteger.MAX_VALUEifsetMaxResults(int)was not applied to the query object.- Returns:
- maximum number of results
- Since:
- 2.0
 
- 
setFirstResultSet the position of the first result to retrieve.- Parameters:
- startPosition- position of the first result, numbered from 0
- Returns:
- the same query instance
- Throws:
- IllegalArgumentException- if the argument is negative
 
- 
getFirstResultint getFirstResult()The position of the first result the query object was set to retrieve. Returns0ifsetFirstResultwas not applied to the query object.- Returns:
- position of the first result
- Since:
- 2.0
 
- 
setHintSet 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 the property or hint
- value- value for the property or hint
- Returns:
- the same query instance
- Throws:
- IllegalArgumentException- if the second argument is not valid for the implementation
 
- 
getHintsGet the properties and hints and associated values that are in effect for the query instance.- Returns:
- query properties and hints
- Since:
- 2.0
 
- 
setParameterBind the value of aParameterobject.- Parameters:
- param- parameter object
- value- parameter value
- Returns:
- the same query instance
- Throws:
- IllegalArgumentException- if the parameter does not correspond to a parameter of the query
- Since:
- 2.0
 
- 
setParameter@Deprecated(since="3.2") Query setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType) Deprecated.Newly-written code should use the date/time types defined injava.time.- Parameters:
- param- parameter object
- value- parameter value
- temporalType- temporal type
- Returns:
- the same query instance
- Throws:
- IllegalArgumentException- if the parameter does not correspond to a parameter of the query
- Since:
- 2.0
 
- 
setParameter@Deprecated(since="3.2") Query setParameter(Parameter<Date> param, Date value, TemporalType temporalType) Deprecated.Newly-written code should use the date/time types defined injava.time.- Parameters:
- param- parameter object
- value- parameter value
- temporalType- temporal type
- Returns:
- the same query instance
- Throws:
- IllegalArgumentException- if the parameter does not correspond to a parameter of the query
- Since:
- 2.0
 
- 
setParameterBind an argument value to a named parameter.- Parameters:
- name- parameter name
- value- parameter value
- Returns:
- 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
 
- 
setParameterDeprecated.Newly-written code should use the date/time types defined injava.time.Bind an instance ofCalendarto a named parameter.- Parameters:
- name- parameter name
- value- parameter value
- temporalType- temporal type
- Returns:
- 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
 
- 
setParameterDeprecated.Newly-written code should use the date/time types defined injava.time.Bind an instance ofDateto a named parameter.- Parameters:
- name- parameter name
- value- parameter value
- temporalType- temporal type
- Returns:
- 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
 
- 
setParameterBind an argument value to a positional parameter.- Parameters:
- position- position
- value- parameter value
- Returns:
- 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
 
- 
setParameter@Deprecated(since="3.2") Query setParameter(int position, Calendar value, TemporalType temporalType) Deprecated.Newly-written code should use the date/time types defined injava.time.Bind an instance ofCalendarto a positional parameter.- Parameters:
- position- position
- value- parameter value
- temporalType- temporal type
- Returns:
- 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
 
- 
setParameterDeprecated.Newly-written code should use the date/time types defined injava.time.Bind an instance ofDateto a positional parameter.- Parameters:
- position- position
- value- parameter value
- temporalType- temporal type
- Returns:
- 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
 
- 
getParametersGet 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.- Returns:
- set of the parameter objects
- Throws:
- IllegalStateException- if invoked on a native query when the implementation does not support this use
- Since:
- 2.0
 
- 
getParameterGet 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
- Returns:
- 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
- Since:
- 2.0
 
- 
getParameterGet 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
- Returns:
- 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
- Since:
- 2.0
 
- 
getParameterGet 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
- Returns:
- 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
- Since:
- 2.0
 
- 
getParameterGet 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
- Returns:
- 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
- Since:
- 2.0
 
- 
isBoundReturn a boolean indicating whether a value has been bound to the parameter.- Parameters:
- param- parameter object
- Returns:
- boolean indicating whether parameter has been bound
- Since:
- 2.0
 
- 
getParameterValueReturn the input value bound to the parameter. (Note that OUT parameters are unbound.)- Parameters:
- param- parameter object
- Returns:
- parameter value
- Throws:
- IllegalArgumentException- if the parameter is not a parameter of the query
- IllegalStateException- if the parameter has not been bound
- Since:
- 2.0
 
- 
getParameterValueReturn the input value bound to the named parameter. (Note that OUT parameters are unbound.)- Parameters:
- name- parameter name
- Returns:
- parameter value
- Throws:
- IllegalStateException- if the parameter has not been bound
- IllegalArgumentException- if the parameter of the specified name does not exist
- Since:
- 2.0
 
- 
getParameterValueReturn the input value bound to the positional parameter. (Note that OUT parameters are unbound.)- Parameters:
- position- position
- Returns:
- parameter value
- Throws:
- IllegalStateException- if the parameter has not been bound
- IllegalArgumentException- if the parameter with the specified position does not exist
- Since:
- 2.0
 
- 
setFlushModeSet 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
- Returns:
- the same query instance
 
- 
getFlushModeFlushModeType getFlushMode()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.- Returns:
- flush mode
- Since:
- 2.0
 
- 
setLockModeSet the lock mode type to be used for the query execution.- Parameters:
- lockMode- lock mode
- Returns:
- the same query instance
- Throws:
- IllegalStateException- if the query is found not to be a Jakarta Persistence query language SELECT query or a- CriteriaQueryquery
- Since:
- 2.0
 
- 
getLockModeLockModeType getLockMode()Get the current lock mode for the query. Returns null if a lock mode has not been set on the query object.- Returns:
- lock mode
- Throws:
- IllegalStateException- if the query is found not to be a Jakarta Persistence query language SELECT query or a- CriteriaQueryquery
- Since:
- 2.0
 
- 
setCacheRetrieveModeSet the cache retrieval mode that is in effect during query execution. This cache retrieval mode overrides the cache retrieve mode in use by the entity manager.- Parameters:
- cacheRetrieveMode- cache retrieval mode
- Returns:
- the same query instance
- Since:
- 3.2
 
- 
setCacheStoreModeSet the cache storage mode that is in effect during query execution. This cache storage mode overrides the cache storage mode in use by the entity manager.- Parameters:
- cacheStoreMode- cache storage mode
- Returns:
- the same query instance
- Since:
- 3.2
 
- 
getCacheRetrieveModeCacheRetrieveMode getCacheRetrieveMode()The cache retrieval mode that will be in effect during query execution.- Since:
- 3.2
 
- 
getCacheStoreModeCacheStoreMode getCacheStoreMode()The cache storage mode that will be in effect during query execution.- Since:
- 3.2
 
- 
setTimeoutSet the query timeout, in milliseconds. This is a hint, and is an alternative to setting the hintjakarta.persistence.query.timeout.- Parameters:
- timeout- the timeout, in milliseconds, or null to indicate no timeout
- Returns:
- the same query instance
- Since:
- 3.2
 
- 
getTimeoutInteger getTimeout()The query timeout.- Since:
- 3.2
 
- 
unwrapReturn an object of the specified type to allow access to a provider-specific API. If the provider implementation ofQuerydoes not support the given type, thePersistenceExceptionis thrown.- Parameters:
- cls- the type of the object to be returned. This is usually either the underlying class implementing- Queryor an interface it implements.
- Returns:
- an instance of the specified class
- Throws:
- PersistenceException- if the provider does not support the given type
- Since:
- 2.0
 
 
- 
java.time.