Interface Statement
- All Superinterfaces:
Query
Interface used to control the execution of executable statements.
In the Jakarta Persistence query language only
UPDATE and
DELETE statements are executable statements. On the other
hand, a native SQL statement is considered executable if it returns
a row count instead of a result set.- Since:
- 4.0
- See Also:
- API note:
- Just like any other data access API, including JDBC itself, native SQL statement strings and Jakarta Persistence query language strings executed via this API must never be composed by concatenating user input or other untrusted data. User input should be properly validated and passed as a parameter to a parameterized statement string. In particular, native SQL statement strings executed via this API are typically executed verbatim with no additional validation or sanitization beyond that performed by the JDBC driver.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceAn option influencing execution of a statement. -
Method Summary
Modifier and TypeMethodDescriptionaddOption(Statement.Option option) Specify an option influencing execution of this statement, overwriting any existing option of the same type.intexecute()Execute anUPDATEorDELETEstatement or a native SQL statement that returns a row count.Get the options influencing execution of this statement.<P> StatementsetConvertedParameter(int position, P value, Class<? extends AttributeConverter<P, ?>> converter) Bind an argument value to a named parameter, explicitly specifying an attribute converter to use.<P> StatementsetConvertedParameter(String name, P value, Class<? extends AttributeConverter<P, ?>> converter) Bind an argument value to a named parameter, explicitly specifying an attribute converter to use.setFlushMode(FlushModeType flushMode) Deprecated.Set a query property or hint.setParameter(int position, Object value) Bind an argument value to a positional parameter.setParameter(int position, Calendar value, TemporalType temporalType) Deprecated.Newly written code should use the date/time types defined injava.time.setParameter(int position, Date value, TemporalType temporalType) Deprecated.Newly written code should use the date/time types defined injava.time.<P> StatementsetParameter(int position, P value, Type<P> type) Bind an argument value to a positional parameter, explicitly specifying the parameter type.<P> StatementsetParameter(int position, P value, Class<P> type) Bind an argument value to a positional parameter, explicitly specifying the parameter type.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> StatementsetParameter(Parameter<T> parameter, T value) Bind an argument to a parameter of this query respresented as 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.<P> StatementsetParameter(String name, P value, Type<P> type) Bind an argument value to a named parameter, explicitly specifying the parameter type.<P> StatementsetParameter(String name, P value, Class<P> type) Bind an argument value to a named parameter, explicitly specifying the parameter type.setQueryFlushMode(QueryFlushMode flushMode) Set the query flush mode to be used when the query is executed.setTimeout(Timeout timeout) Set the query timeout.setTimeout(Integer timeout) Set the query timeout, in milliseconds.Methods inherited from interface Query
executeUpdate, getCacheRetrieveMode, getCacheStoreMode, getFirstResult, getFlushMode, getHints, getLockMode, getMaxResults, getParameter, getParameter, getParameter, getParameter, getParameters, getParameterValue, getParameterValue, getParameterValue, getQueryFlushMode, getResultList, getResultStream, getSingleResult, getSingleResultOrNull, getTimeout, isBound, setCacheRetrieveMode, setCacheStoreMode, setFirstResult, setLockMode, setMaxResults, unwrap
-
Method Details
-
execute
int execute()Execute anUPDATEorDELETEstatement or a native SQL statement that returns a row count.After execution of a bulk update or delete operation, the persistence provider is not required to resynchronize state held in memory with the effects of the operation on data held in the database. However, when this method is called within a transaction, the persistence context is joined to the transaction, and
FlushModeType.AUTOis in effect, the persistence provider must ensure that every modification to the state of every entity associated with the persistence context which could possibly alter the effects of the bulk update or delete operation is visible to the processing of the operation.- Returns:
- the number of entities updated or deleted, or the row count of the native SQL statement
- Throws:
IllegalStateException- if called for a Jakarta Persistence query language SELECT statement or for a criteria queryTransactionRequiredException- if there is no transaction or the persistence context has not been joined to the transactionQueryTimeoutException- if the statement execution exceeds the query timeout value set and only the statement is rolled backPersistenceException- if the query execution exceeds the query timeout value set and the transaction is rolled backPersistenceException- if the flush failsOptimisticLockException- if an optimistic locking conflict is detected during the flush
-
setHint
Set a query property or hint. Properties defined by this specification must be observed by the persistence 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.- Specified by:
setHintin interfaceQuery- Parameters:
hintName- The name of the property or hintvalue- The value for the property or hint- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the given property or hint name is recognized by the provider, but the second argument is not a legal value for the given property or hint- API note:
- The use of named hints lacks type safety compared to the use of options.
-
setTimeout
Set the query timeout, in milliseconds. This is a hint, and is an alternative to setting the hintjakarta.persistence.query.timeout.- Specified by:
setTimeoutin interfaceQuery- Parameters:
timeout- The timeout, in milliseconds, or null to indicate no timeout- Returns:
- the same statement instance
- Since:
- 3.2
-
setTimeout
Set the query timeout. This is a hint.- Specified by:
setTimeoutin interfaceQuery- Parameters:
timeout- the timeout, or null to indicate no timeout- Returns:
- the same statement instance
- Since:
- 4.0
-
setQueryFlushMode
Set the query flush mode to be used when the query is executed. This flush mode overrides the flush mode type of the entity manager.- Specified by:
setQueryFlushModein interfaceQuery- Parameters:
flushMode- The new flush mode- Returns:
- the same statement instance
- Since:
- 4.0
-
setFlushMode
Deprecated.Set the flush mode type to be used when the query is executed. This flush mode overrides the flush mode type of the entity manager.- Specified by:
setFlushModein interfaceQuery- Parameters:
flushMode- The new flush mode- Returns:
- the same statement instance
-
setParameter
Bind an argument to a parameter of this query respresented as aParameterobject.- Specified by:
setParameterin interfaceQuery- Type Parameters:
T- the parameter type- Parameters:
parameter- The parameter objectvalue- The argument to the parameter- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the parameter does not correspond to a parameter of the query- Since:
- 2.0
-
setParameter
@Deprecated(since="3.2") @Nonnull Statement setParameter(@Nonnull Parameter<Calendar> param, @Nullable Calendar value, @Nonnull TemporalType temporalType) Deprecated.Newly written code should use the date/time types defined injava.time.- Specified by:
setParameterin interfaceQuery- Parameters:
param- The parameter objectvalue- The argument to the parametertemporalType- A temporal type- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the parameter does not correspond to a parameter of the query- Since:
- 2.0
-
setParameter
@Deprecated(since="3.2") @Nonnull Statement setParameter(@Nonnull Parameter<Date> param, @Nullable Date value, @Nonnull TemporalType temporalType) Deprecated.Newly written code should use the date/time types defined injava.time.- Specified by:
setParameterin interfaceQuery- Parameters:
param- The parameter objectvalue- The argument to the parametertemporalType- A temporal type- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the parameter does not correspond to a parameter of the query- Since:
- 2.0
-
setParameter
Bind an argument value to a named parameter.- Specified by:
setParameterin interfaceQuery- Parameters:
name- The name of the parametervalue- The argument to the parameter- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the parameter name does not correspond to a parameter of the query, or if the argument is of incompatible type
-
setParameter
@Nonnull <P> Statement setParameter(@Nonnull String name, @Nullable P value, @Nonnull Class<P> type) Bind an argument value to a named parameter, explicitly specifying the parameter type. This is most useful when the argument might benull, especially in the case of a native query.em.createNativeStatement("update books set pub_date = :date where isbn = :ISBN") .setParameter("date", optionalPublicationDate, LocalDate.class) .setParameter("ISBN", isbn) .execute();- Specified by:
setParameterin interfaceQuery- Type Parameters:
P- the parameter type- Parameters:
name- The name of the parametervalue- The argument to the parametertype- A class object representing the parameter type- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the parameter name does not correspond to a parameter of the query, or if the argument is of incompatible type- Since:
- 4.0
-
setParameter
Bind an argument value to a named parameter, explicitly specifying the parameter type. This is most useful when the binding is affected by an attribute converter.em.createNativeStatement("update books set pub_date = :date where isbn = :ISBN") .setParameter("date", optionalPublicationDate, Book_.publicationDate.getType()) .setParameter("ISBN", isbn, Book_.isbn.getType()) .execute();- Specified by:
setParameterin interfaceQuery- Type Parameters:
P- the parameter type- Parameters:
name- The name of the parametervalue- The argument to the parametertype- TheTypeof the parameter- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the parameter name does not correspond to a parameter of the query, or if the argument is of incompatible type- Since:
- 4.0
-
setConvertedParameter
@Nonnull <P> Statement setConvertedParameter(@Nonnull String name, @Nullable P value, @Nonnull Class<? extends AttributeConverter<P, ?>> converter) Bind an argument value to a named parameter, explicitly specifying an attribute converter to use.em.createNativeStatement("update books set pub_date = :date where isbn = :ISBN") .setParameter("date", publicationDate) .setParameter("ISBN", isbn, IsbnConverter.class) .execute();- Specified by:
setConvertedParameterin interfaceQuery- Type Parameters:
P- the parameter type- Parameters:
name- The name of the parametervalue- The argument to the parameterconverter- The class of the attribute converter- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the parameter name does not correspond to a parameter of the query, or if the argument is of incompatible type- Since:
- 4.0
-
setParameter
@Deprecated(since="3.2") @Nonnull Statement setParameter(@Nonnull String name, @Nullable Calendar value, @Nonnull TemporalType temporalType) Deprecated.Newly written code should use the date/time types defined injava.time.Bind an instance ofCalendarto a named parameter.- Specified by:
setParameterin interfaceQuery- Parameters:
name- The name of the parametervalue- The argument to the parametertemporalType- A temporal type- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the parameter name does not correspond to a parameter of the query, or if the value argument is of incompatible type
-
setParameter
@Deprecated(since="3.2") @Nonnull Statement setParameter(@Nonnull String name, @Nullable Date value, @Nonnull TemporalType temporalType) Deprecated.Newly written code should use the date/time types defined injava.time.Bind an instance ofDateto a named parameter.- Specified by:
setParameterin interfaceQuery- Parameters:
name- The name of the parametervalue- The argument to the parametertemporalType- A temporal type- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the parameter name does not correspond to a parameter of the query, or if the value argument is of incompatible type
-
setParameter
Bind an argument value to a positional parameter.- Specified by:
setParameterin interfaceQuery- Parameters:
position- The parameter positionvalue- The argument to the parameter- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the given position does not correspond to a positional parameter of the query, or if the argument is of incompatible type
-
setParameter
Bind an argument value to a positional parameter, explicitly specifying the parameter type. This is most useful when the argument might benull, especially in the case of a native SQL query.em.createNativeStatement("update books set pub_date = ?1 where isbn = ?2") .setParameter(1, optionalPublicationDate, LocalDate.class) .setParameter(2, isbn) .execute();- Specified by:
setParameterin interfaceQuery- Type Parameters:
P- the parameter type- Parameters:
position- The parameter positionvalue- The argument to the parametertype- A class object representing the parameter type- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the given position does not correspond to a parameter of the query, or if the argument is of incompatible type- Since:
- 4.0
-
setParameter
Bind an argument value to a positional parameter, explicitly specifying the parameter type. This is most useful when the binding is affected by an attribute converter.em.createNativeStatement("update books set pub_date = ?1 where isbn = ?2") .setParameter(1, optionalPublicationDate, Book_.publicationDate.getType()) .setParameter(2, isbn, Book_.isbn.getType()) .execute();- Specified by:
setParameterin interfaceQuery- Type Parameters:
P- the parameter type- Parameters:
position- The parameter positionvalue- The argument to the parametertype- TheTypeof the parameter- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the given position does not correspond to a positional parameter of the query, or if the argument is of incompatible type- Since:
- 4.0
-
setConvertedParameter
@Nonnull <P> Statement setConvertedParameter(int position, @Nullable P value, @Nonnull Class<? extends AttributeConverter<P, ?>> converter) Bind an argument value to a named parameter, explicitly specifying an attribute converter to use.em.createNativeStatement("update books set pub_date = ?1 where isbn = ?2") .setParameter(1, publicationDate) .setParameter(2, isbn, IsbnConverter.class) .execute();- Specified by:
setConvertedParameterin interfaceQuery- Type Parameters:
P- the parameter type- Parameters:
position- The parameter positionvalue- The argument to the parameterconverter- The class of the attribute converter- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the given position does not correspond to a parameter of the query, or if the argument is of incompatible type- Since:
- 4.0
-
setParameter
@Deprecated(since="3.2") @Nonnull Statement setParameter(int position, @Nullable Calendar value, @Nonnull TemporalType temporalType) Deprecated.Newly written code should use the date/time types defined injava.time.Bind an instance ofCalendarto a positional parameter.- Specified by:
setParameterin interfaceQuery- Parameters:
position- The parameter positionvalue- The argument to the parametertemporalType- A temporal type- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the given position does not correspond to a positional parameter of the query, or if the argument is of incompatible type
-
setParameter
@Deprecated(since="3.2") @Nonnull Statement setParameter(int position, @Nullable Date value, @Nonnull TemporalType temporalType) Deprecated.Newly written code should use the date/time types defined injava.time.Bind an instance ofDateto a positional parameter.- Specified by:
setParameterin interfaceQuery- Parameters:
position- The parameter positionvalue- The argument to the parametertemporalType- A temporal type- Returns:
- the same statement instance
- Throws:
IllegalArgumentException- if the given position does not correspond to a positional parameter of the query, or if the argument is of incompatible type
-
addOption
Specify an option influencing execution of this statement, overwriting any existing option of the same type.- Parameters:
option- the option- Returns:
- the same statement instance
- Since:
- 4.0
-
getOptions
Get the options influencing execution of this statement. The returned set includes options set viaaddOption(Statement.Option), along with options specified viasetTimeout(Integer)orsetQueryFlushMode(QueryFlushMode). Mutation of the returned set does not affect the options of the statement.- Returns:
- the options for this statement
- Since:
- 4.0
-
setQueryFlushMode(QueryFlushMode).