StoredProcedureQuery
- Super Interfaces:
Query
Interface used to control stored procedure query execution.
Stored procedure query execution may be controlled in accordance with the following:
- The
setParameter
methods are used to set the values of all requiredIN
andINOUT
parameters. It is not required to set the values of stored procedure parameters for which default values have been defined by the stored procedure. - When
getResultList
andgetSingleResult
are called on aStoredProcedureQuery
object, the provider will callexecute
on an unexecuted stored procedure query before processinggetResultList
orgetSingleResult
. - When
executeUpdate
is called on aStoredProcedureQuery
object, the provider will callexecute
on an unexecuted stored procedure query followed bygetUpdateCount
. The results ofexecuteUpdate
will be those ofgetUpdateCount
. - The
execute
method supports both the simple case where scalar results are passed back only viaINOUT
andOUT
parameters as well as the most general case (multiple result sets and/or update counts, possibly also in combination with output parameter values). - The
execute
method returns true if the first result is a result set, and false if it is an update count or there are no results other than throughINOUT
andOUT
parameters, if any. - If the
execute
method returns true, the pending result set can be obtained by callinggetResultList
orgetSingleResult
. - The
hasMoreResults
method can then be used to test for further results. - If
execute
orhasMoreResults
returns false, thegetUpdateCount
method can be called to obtain the pending result if it is an update count. ThegetUpdateCount
method will return either the update count (zero or greater) or -1 if there is no update count (i.e., either the next result is a result set or there is no next update count). - For portability, results that correspond to JDBC result sets and update counts need to be processed before the values of any
INOUT
orOUT
parameters are extracted. - After results returned through
getResultList
andgetUpdateCount
have been exhausted, results returned throughINOUT
andOUT
parameters can be retrieved. - The
getOutputParameterValue
methods are used to retrieve the values passed back from the procedure throughINOUT
andOUT
parameters. - When using
REF_CURSOR
parameters for result sets the update counts should be exhausted before callinggetResultList
to retrieve the result set. Alternatively, theREF_CURSOR
result set can be retrieved throughgetOutputParameterValue
. Result set mappings will be applied to results corresponding toREF_CURSOR
parameters in the order theREF_CURSOR
parameters were registered with the query. - In the simplest case, where results are returned only via
INOUT
andOUT
parameters,execute
can be followed immediately by calls togetOutputParameterValue
.
- Since:
- JPA 2.1
Public Methods
boolean execute()
- Return:
- true if first result corresponds to result set
- Throws:
- QueryTimeoutException - if the query 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
- Since:
- JPA 2.1
int executeUpdate()
The provider will call
execute
on the query if needed. - Return:
- the update count or -1 if there is no pending result or if the next result is not an update count.
- Throws:
- 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
- QueryTimeoutException - if the statement execution exceeds the query timeout value set and only the statement is rolled back
- Since:
- JPA 2.1
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 Java Persistence query language SELECT query or a Criteria API query
- Inherited from:
Query
- Since:
- JPA 2.0
int getMaxResults()
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
Object getOutputParameterValue(String parameterName)
For portability, all results corresponding to result sets and update counts must be retrieved before the values of output parameters.
- Parameters:
parameterName
- name of the parameter as registered or specified in metadata
- Return:
- the result that is passed back through the parameter
- Throws:
- IllegalArgumentException - if the parameter name does not correspond to a parameter of the query or is not an INOUT or OUT parameter
- Since:
- JPA 2.1
Object getOutputParameterValue(int position)
For portability, all results corresponding to result sets and update counts must be retrieved before the values of output parameters.
- Parameters:
position
- parameter position
- Return:
- the result that is passed back through the parameter
- Throws:
- IllegalArgumentException - if the position does not correspond to a parameter of the query or is not an INOUT or OUT parameter
- Since:
- JPA 2.1
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 Java 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 Java 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 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 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 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 getResultList()
The provider will call
execute
on the query if needed. A REF_CURSOR
result set, if any, will be retrieved in the order the REF_CURSOR
parameter was registered with the query. - Return:
- a list of the results or null is the next item is not a result set
- Throws:
- QueryTimeoutException - if the query 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
- Since:
- JPA 2.1
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
- QueryTimeoutException - if the query execution exceeds the query timeout value set and only the statement is rolled back
- Inherited from:
Query
- See Also:
Stream
getResultList()
- Since:
- JPA 2.2
Object getSingleResult()
The provider will call
execute
on the query if needed. A REF_CURSOR
result set, if any, will be retrieved in the order the REF_CURSOR
parameter was registered with the query. - Return:
- the result or null if the next item is not a result set
- Throws:
- NoResultException - if there is no result in the next result set
- NonUniqueResultException - if more than one result
- QueryTimeoutException - if the query 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
- NonUniqueResultException - if more than one result
- Since:
- JPA 2.1
int getUpdateCount()
- Return:
- update count or -1 if there is no pending result or if the next result is not an update count
- Throws:
- QueryTimeoutException - if the query 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
- Since:
- JPA 2.1
boolean hasMoreResults()
- Return:
- true if next result corresponds to result set
- Throws:
- QueryTimeoutException - if the query 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
- Since:
- JPA 2.1
- Parameters:
param
- parameter object
- Return:
- boolean indicating whether parameter has been bound
- Inherited from:
Query
- Since:
- JPA 2.0
StoredProcedureQuery registerStoredProcedureParameter(String parameterName, Class type, ParameterMode mode)
- Parameters:
parameterName
- name of the parameter as registered or specified in metadatatype
- type of the parametermode
- parameter mode
- Return:
- the same query instance
- Since:
- JPA 2.1
All parameters must be registered.
- Parameters:
position
- parameter positiontype
- type of the parametermode
- parameter mode
- Return:
- the same query instance
- Since:
- JPA 2.1
- Parameters:
startPosition
- position of the first result, numbered from 0
- Return:
- the same query instance
- Throws:
- IllegalArgumentException - if the argument is negative
- Inherited from:
Query
- Since:
- JPA 1.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.1
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, this hint may or may not be observed.
- Parameters:
hintName
- name of the 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.1
- 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
- Inherited from:
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
- Inherited from:
Query
- Since:
- JPA 1.0
StoredProcedureQuery setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType)
- Parameters:
param
- parameter objectvalue
- parameter valuetemporalType
- temporal type
- Return:
- the same query instance
- Throws:
- IllegalArgumentException - if the parameter does not correspond to a parameter of the query
- Since:
- JPA 2.1
- Parameters:
param
- parameter objectvalue
- parameter valuetemporalType
- temporal type
- Return:
- the same query instance
- Throws:
- IllegalArgumentException - if the parameter does not correspond to a parameter of the query
- Since:
- JPA 2.1
- 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.1
- Parameters:
name
- parameter namevalue
- parameter valuetemporalType
- 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.1
- Parameters:
name
- parameter namevalue
- parameter valuetemporalType
- 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.1
- 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.1
- Parameters:
position
- positionvalue
- parameter valuetemporalType
- 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.1
- Parameters:
position
- positionvalue
- parameter valuetemporalType
- 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.1
- 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.1
T unwrap(Class<T> cls)
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