Interface CommonQueryContract
- All Superinterfaces:
Query
- All Known Subinterfaces:
CommonQueryContractImplementor, MutationOrSelectionQuery, MutationQuery, MutationQueryImplementor<T>, NativeQuery<T>, NativeQueryImplementor<R>, ProcedureCall, ProcedureCallImplementor<R>, Query<T>, QueryImplementor<T>, SelectionQuery<R>, SelectionQueryImplementor<R>
- queries written in HQL, or JPQL,
- queries written in the native SQL dialect of the database,
- criteria queries, and
- stored procedure calls.
Also acts as the primary extension point for the Jakarta Persistence Query hierarchy.
Queries may have parameters, either ordinal or named, and the various
setParameter() operations of this interface allow an argument to be
bound to a parameter. It's not usually necessary to explicitly specify the type
of an argument, but in rare cases where this is needed:
- an instance of an appropriate metamodel
Typemay be passed tosetParameter(int, Object, Type), or - the argument may be wrapped in a
TypedParameterValue. (For JPA users, this second option avoids the need to cast theQueryto a Hibernate-specific type.)
For example:
session.createSelectionQuery("from Person where address = :address", Person.class)
.setParameter("address", address, Person_.address.getType())
.getResultList()
entityManager.createQuery( "from Person where address = :address", Person.class)
.setParameter("address", TypedParameterValue.of(Person_.address.getType(), address))
.getResultList()
The operation setQueryFlushMode(QueryFlushMode) allows a temporary flush
mode to be specified, which is in effect only during the execution of this query.
Setting the query flush mode does not affect the flush
mode of other operations performed via the parent session.
This operation is usually used as follows:
query.setQueryFlushMode(NO_FLUSH).getResultList()The call to
setQueryFlushMode(NO_FLUSH) disables the usual automatic flush
operation that occurs before query execution.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionaddQueryHint(String hint) Add a database query hint to the SQL query.Obtain the comment currently associated with this query.TheFlushModein effect for this query, taking into account both the query flush mode and the flush mode of the session.Get theParameterMetadataobject representing the parameters of this query, and providing access to theQueryParameters.TheQueryFlushModein effect for this query.Obtain the JDBC query timeout that will be used when executing the query.setComment(String comment) Set the comment for this query.setConvertedParameter(int position, P value, Class<? extends AttributeConverter<P, ?>> converter) setConvertedParameter(String name, P value, Class<? extends AttributeConverter<P, ?>> converter) Set a hint.setParameter(int parameter, Object value) Bind the given argument to an ordinal query parameter.setParameter(int parameter, Instant value, TemporalType temporalType) Deprecated.setParameter(int parameter, Calendar value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecatedsetParameter(int parameter, Date value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecatedsetParameter(int parameter, P value, Type<P> type) Bind the given argument to an ordinal query parameter using the givenType.setParameter(int parameter, P value, Class<P> type) setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecatedsetParameter(Parameter<Date> param, Date value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecatedsetParameter(Parameter<T> param, T value) setParameter(String parameter, Object value) Bind the given argument to a named query parameter.setParameter(String parameter, Instant value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecatedsetParameter(String parameter, Calendar value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecatedsetParameter(String parameter, Date value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecatedsetParameter(String parameter, P value, Type<P> type) Bind the given argument to a named query parameter using the givenType.setParameter(String parameter, P value, Class<P> type) setParameter(QueryParameter<P> parameter, P val, Type<P> type) Bind an argument to the query parameter represented by the givenQueryParameter, using the givenType.setParameter(QueryParameter<P> parameter, P value, Class<P> type) Bind an argument to the query parameter represented by the givenQueryParameter, using the givenClassreference to attempt to infer theTypeto use.setParameter(QueryParameter<T> parameter, T value) Bind an argument to the query parameter represented by the givenQueryParameter.setParameterList(int parameter, Object[] values) Bind multiple arguments to an ordinal query parameter.setParameterList(int parameter, Collection values) Bind multiple arguments to an ordinal query parameter.setParameterList(int parameter, Collection<? extends P> values, Type<P> type) Bind multiple arguments to an ordinal query parameter using the givenType.setParameterList(int parameter, Collection<? extends P> values, Class<P> javaType) setParameterList(int parameter, P[] values, Type<P> type) Bind multiple arguments to an ordinal query parameter using the givenType.setParameterList(int parameter, P[] values, Class<P> javaType) setParameterList(String parameter, Object[] values) Bind multiple arguments to a named query parameter.setParameterList(String parameter, Collection values) Bind multiple arguments to a named query parameter.setParameterList(String parameter, Collection<? extends P> values, Type<P> type) Bind multiple arguments to a named query parameter using the givenType.setParameterList(String parameter, Collection<? extends P> values, Class<P> javaType) Bind multiple arguments to a named query parameter using the givenClassreference to attempt to infer theTypeIf unable to infer an appropriateType, fall back tosetParameterList(String, Collection).setParameterList(String parameter, P[] values, Type<P> type) Bind multiple arguments to a named query parameter using the givenType.setParameterList(String parameter, P[] values, Class<P> javaType) Bind multiple arguments to a named query parameter using the given Class reference to attempt to determine theTypeto use.setParameterList(QueryParameter<P> parameter, Collection<? extends P> values) Bind multiple arguments to the query parameter represented by the givenQueryParameter.setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, Type<P> type) Bind multiple arguments to the query parameter represented by the givenQueryParameter, using the givenType.setParameterList(QueryParameter<P> parameter, Collection<? extends P> values, Class<P> javaType) Bind multiple arguments to the query parameter represented by the givenQueryParameterusing the givenClassreference to attempt to infer theTypeto use.setParameterList(QueryParameter<P> parameter, P[] values) Bind multiple arguments to the query parameter represented by the givenQueryParameter.setParameterList(QueryParameter<P> parameter, P[] values, Type<P> type) Bind multiple arguments to the query parameter represented by the givenQueryParameter, using the given theType.setParameterList(QueryParameter<P> parameter, P[] values, Class<P> javaType) Bind multiple arguments to the query parameter represented by the givenQueryParameterusing the givenClassreference to attempt to infer theTypeto use.setParameters(Object... arguments) setProperties(Object bean) Bind the property values of the given bean to named parameters of the query, matching property names with parameter names and mapping property types to Hibernate types using heuristics.setProperties(Map bean) Bind the values of the givenMapto named parameters of the query, matching key names with parameter names and mapping value types to Hibernate types using heuristics.setQueryFlushMode(QueryFlushMode queryFlushMode) Set theQueryFlushModeto use for this query.setTimeout(int timeout) Specify a JDBC query timeout to use when executing the query.setTimeout(Timeout timeout) Specify a JDBC query timeout to use when executing the query.setTimeout(Integer timeout) Specify a JDBC query timeout to use when executing the query.Methods inherited from interface Query
executeUpdate, getCacheRetrieveMode, getCacheStoreMode, getFirstResult, getFlushMode, getHints, getLockMode, getMaxResults, getParameter, getParameter, getParameter, getParameter, getParameters, getParameterValue, getParameterValue, getParameterValue, getResultList, getResultStream, getSingleResult, getSingleResultOrNull, isBound, setCacheRetrieveMode, setCacheStoreMode, setFirstResult, setFlushMode, setLockMode, setMaxResults, unwrap
-
Method Details
-
getSession
- Returns:
- The producer of this query
-
getQueryFlushMode
TheQueryFlushModein effect for this query.By default, this is
QueryFlushMode.DEFAULT, and theFlushModeof the owning session determines whether the session is flushed before execution of the query.A
QueryFlushModeonly affects stateful sessions. A stateless session has no persistence context to flush.- Specified by:
getQueryFlushModein interfaceQuery- Since:
- 7.0
- See Also:
-
setQueryFlushMode
Set theQueryFlushModeto use for this query.A
QueryFlushModeonly affects stateful sessions. A stateless session has no persistence context to flush.- Specified by:
setQueryFlushModein interfaceQuery- Since:
- 7.0
- See Also:
-
getEffectiveFlushMode
TheFlushModein effect for this query, taking into account both the query flush mode and the flush mode of the session. By default, the query inherits theFlushModeof the session from which it originates.A
FlushModeonly affects stateful sessions. A stateless session has no persistence context to flush.- Since:
- 8.0
- See Also:
-
getComment
Obtain the comment currently associated with this query.If SQL commenting is enabled, the comment will be added to the SQL query sent to the database, which may be useful for identifying the source of troublesome queries.
SQL commenting may be enabled using the configuration property "hibernate.use_sql_comments".
-
setComment
Set the comment for this query.If SQL commenting is enabled, the comment will be added to the SQL query sent to the database, which may be useful for identifying the source of troublesome queries.
SQL commenting may be enabled using the configuration property "hibernate.use_sql_comments".
- See Also:
-
addQueryHint
Add a database query hint to the SQL query.Multiple query hints may be specified. The operation
Dialect.getQueryHintString(String, List)determines how the hint is actually added to the SQL query. -
getTimeout
Obtain the JDBC query timeout that will be used when executing the query.See Timeouts for discussion of the "magic values" accepted here.
- Specified by:
getTimeoutin interfaceQuery- See Also:
- API Note:
- Since this method is inherited from JPA, the timeout is expressed in milliseconds.
-
setTimeout
Specify a JDBC query timeout to use when executing the query.- See Also:
- API Note:
- Since this is a legacy method of Hibernate, the timeout value is expressed in seconds.
-
setTimeout
Specify a JDBC query timeout to use when executing the query.- Specified by:
setTimeoutin interfaceQuery- Since:
- 7.0
- See Also:
- API Note:
- Since this method is inherited from JPA, the timeout is expressed in milliseconds.
-
setTimeout
Specify a JDBC query timeout to use when executing the query.- Specified by:
setTimeoutin interfaceQuery- Since:
- 7.0
-
setHint
Set a hint. Hints are a JPA-standard way to control provider-specific behavior affecting execution of the query. Clients of this native Hibernate API should make use of type safe operations of this interface and of its subtypes. For example,SelectionQuery.setCacheRegion(String)is preferred overHibernateHints.HINT_CACHE_REGION.Since JPA 4,
TypedQuery.OptionandStatement.Optioncompete with query hints, offering a more type safe way to accommodate vendor extensions.The hints understood by Hibernate are enumerated by
AvailableHints.- Specified by:
setHintin interfaceQuery- See Also:
- API Note:
- Very different from addQueryHint(String) which defines database hints to be applied to the SQL.
-
getParameterMetadata
Get theParameterMetadataobject representing the parameters of this query, and providing access to theQueryParameters.- Since:
- 7.0
-
setParameter
@Nonnull <T> CommonQueryContract setParameter(@Nonnull QueryParameter<T> parameter, @Nullable T value) Bind an argument to the query parameter represented by the givenQueryParameter.If the type of the parameter cannot be inferred from the context in which it occurs, use one of the overloads which accepts a "type".
- Parameters:
parameter- the query parameter mementovalue- the argument, which might be null- Returns:
this, for method chaining- See Also:
-
setParameter
- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameter
Bind the given argument to a named query parameter.If the type of the parameter cannot be inferred from the context in which it occurs, use one of the overloads which accepts a "type", or pass a
TypedParameterValue.- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameter
Bind the given argument to an ordinal query parameter.If the type of the parameter cannot be inferred from the context in which it occurs, use one of the overloads which accepts a "type", or pass a
TypedParameterValue.- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameters
- Specified by:
setParametersin interfaceQuery- See Also:
-
setParameter
@Nonnull <P> CommonQueryContract setParameter(@Nonnull QueryParameter<P> parameter, @Nullable P value, @Nonnull Class<P> type) Bind an argument to the query parameter represented by the givenQueryParameter, using the givenClassreference to attempt to infer theTypeto use. If unable to infer an appropriateType, fall back tosetParameter(QueryParameter, Object).- Parameters:
parameter- the query parameter mementovalue- the argument, which might be nulltype- aTyperepresenting the type of the parameter- Returns:
this, for method chaining- See Also:
-
setParameter
@Nonnull <P> CommonQueryContract setParameter(@Nonnull String parameter, @Nullable P value, @Nonnull Class<P> type) Bind the given argument to a named query parameter using the givenClassreference to attempt to infer theType. If unable to infer an appropriateType, fall back tosetParameter(String, Object).- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameter
@Nonnull <P> CommonQueryContract setParameter(int parameter, @Nullable P value, @Nonnull Class<P> type) Bind the given argument to an ordinal query parameter using the givenClassreference to attempt to infer theType. If unable to infer an appropriateType, fall back tosetParameter(int, Object).- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameter
@Nonnull <P> CommonQueryContract setParameter(@Nonnull QueryParameter<P> parameter, @Nullable P val, @Nonnull Type<P> type) Bind an argument to the query parameter represented by the givenQueryParameter, using the givenType.- Parameters:
parameter- the query parameter mementoval- the argument, which might be nulltype- aTyperepresenting the type of the parameter- Returns:
this, for method chaining
-
setParameter
@Nonnull <P> CommonQueryContract setParameter(@Nonnull String parameter, @Nullable P value, @Nonnull Type<P> type) Bind the given argument to a named query parameter using the givenType.- Specified by:
setParameterin interfaceQuery
-
setParameter
@Nonnull <P> CommonQueryContract setParameter(int parameter, @Nullable P value, @Nonnull Type<P> type) Bind the given argument to an ordinal query parameter using the givenType.- Specified by:
setParameterin interfaceQuery
-
setConvertedParameter
@Nonnull <P> CommonQueryContract setConvertedParameter(@Nonnull String name, @Nullable P value, @Nonnull Class<? extends AttributeConverter<P, ?>> converter) - Specified by:
setConvertedParameterin interfaceQuery- See Also:
-
setConvertedParameter
@Nonnull <P> CommonQueryContract setConvertedParameter(int position, @Nullable P value, @Nonnull Class<? extends AttributeConverter<P, ?>> converter) - Specified by:
setConvertedParameterin interfaceQuery- See Also:
-
setParameterList
@Nonnull CommonQueryContract setParameterList(@Nonnull String parameter, @Nonnull Collection values) Bind multiple arguments to a named query parameter.The "type mapping" for the binding is inferred from the type of the first collection element.
- Returns:
this, for method chaining- See Also:
- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
@Nonnull <P> CommonQueryContract setParameterList(@Nonnull String parameter, @Nonnull Collection<? extends P> values, @Nonnull Class<P> javaType) Bind multiple arguments to a named query parameter using the givenClassreference to attempt to infer theTypeIf unable to infer an appropriateType, fall back tosetParameterList(String, Collection).- Returns:
this, for method chaining- See Also:
- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
@Nonnull <P> CommonQueryContract setParameterList(@Nonnull String parameter, @Nonnull Collection<? extends P> values, @Nonnull Type<P> type) Bind multiple arguments to a named query parameter using the givenType.- Returns:
this, for method chaining- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
Bind multiple arguments to a named query parameter.The "type mapping" for the binding is inferred from the type of the first collection element
- Returns:
this, for method chaining- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
@Nonnull <P> CommonQueryContract setParameterList(@Nonnull String parameter, @Nonnull P[] values, @Nonnull Class<P> javaType) Bind multiple arguments to a named query parameter using the given Class reference to attempt to determine theTypeto use. If unable to determine an appropriateType,setParameterList(String, Collection)is used- Returns:
this, for method chaining- See Also:
- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
@Nonnull <P> CommonQueryContract setParameterList(@Nonnull String parameter, @Nonnull P[] values, @Nonnull Type<P> type) Bind multiple arguments to a named query parameter using the givenType.- Returns:
this, for method chaining- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
Bind multiple arguments to an ordinal query parameter.The "type mapping" for the binding is inferred from the type of the first collection element
- Returns:
this, for method chaining- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
@Nonnull <P> CommonQueryContract setParameterList(int parameter, @Nonnull Collection<? extends P> values, Class<P> javaType) Bind multiple arguments to an ordinal query parameter using the givenClassreference to attempt to infer theType. If unable to infer an appropriateType, fall back tosetParameterList(String, Collection).- Returns:
this, for method chaining- See Also:
- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
@Nonnull <P> CommonQueryContract setParameterList(int parameter, @Nonnull Collection<? extends P> values, Type<P> type) Bind multiple arguments to an ordinal query parameter using the givenType.- Returns:
this, for method chaining- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
Bind multiple arguments to an ordinal query parameter.The "type mapping" for the binding is inferred from the type of the first collection element
- Returns:
this, for method chaining- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
@Nonnull <P> CommonQueryContract setParameterList(int parameter, @Nonnull P[] values, @Nonnull Class<P> javaType) Bind multiple arguments to an ordinal query parameter using the givenClassreference to attempt to infer theType. If unable to infer an appropriateType, fall back tosetParameterList(String, Collection).- Returns:
this, for method chaining- See Also:
- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
@Nonnull <P> CommonQueryContract setParameterList(int parameter, @Nonnull P[] values, @Nonnull Type<P> type) Bind multiple arguments to an ordinal query parameter using the givenType.- Returns:
this, for method chaining- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
@Nonnull <P> CommonQueryContract setParameterList(@Nonnull QueryParameter<P> parameter, @Nonnull Collection<? extends P> values) Bind multiple arguments to the query parameter represented by the givenQueryParameter.The type of the parameter is inferred from the context in which it occurs, and from the type of the first given argument.
- Parameters:
parameter- the parameter mementovalues- a collection of arguments- Returns:
this, for method chaining
-
setParameterList
@Nonnull <P> CommonQueryContract setParameterList(@Nonnull QueryParameter<P> parameter, @Nonnull Collection<? extends P> values, @Nonnull Class<P> javaType) Bind multiple arguments to the query parameter represented by the givenQueryParameterusing the givenClassreference to attempt to infer theTypeto use. If unable to infer an appropriateType, fall back to usingsetParameterList(String, Collection).- Returns:
this, for method chaining- See Also:
- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
@Nonnull <P> CommonQueryContract setParameterList(@Nonnull QueryParameter<P> parameter, @Nonnull Collection<? extends P> values, @Nonnull Type<P> type) Bind multiple arguments to the query parameter represented by the givenQueryParameter, using the givenType.- Returns:
this, for method chaining- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
@Nonnull <P> CommonQueryContract setParameterList(@Nonnull QueryParameter<P> parameter, @Nonnull P[] values) Bind multiple arguments to the query parameter represented by the givenQueryParameter.The type of the parameter is inferred between the context in which it occurs, the type associated with the
QueryParameterand the type of the first given argument.- Parameters:
parameter- the parameter mementovalues- a collection of arguments- Returns:
this, for method chaining
-
setParameterList
@Nonnull <P> CommonQueryContract setParameterList(@Nonnull QueryParameter<P> parameter, @Nonnull P[] values, @Nonnull Class<P> javaType) Bind multiple arguments to the query parameter represented by the givenQueryParameterusing the givenClassreference to attempt to infer theTypeto use. If unable to infer an appropriateType, fall back to usingsetParameterList(String, Collection).- Returns:
this, for method chaining- See Also:
- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
@Nonnull <P> CommonQueryContract setParameterList(@Nonnull QueryParameter<P> parameter, @Nonnull P[] values, @Nonnull Type<P> type) Bind multiple arguments to the query parameter represented by the givenQueryParameter, using the given theType.- Returns:
this, for method chaining- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setProperties
Bind the property values of the given bean to named parameters of the query, matching property names with parameter names and mapping property types to Hibernate types using heuristics.- Parameters:
bean- any JavaBean or POJO- Returns:
this, for method chaining
-
setProperties
Bind the values of the givenMapto named parameters of the query, matching key names with parameter names and mapping value types to Hibernate types using heuristics.- Parameters:
bean- aMapof names to arguments- Returns:
this, for method chaining
-
setParameter
@Deprecated(since="7") @Nonnull CommonQueryContract setParameter(@Nonnull String parameter, @Nullable Instant value, @Nonnull TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecatedBind anInstantto the named query parameter using just the portion indicated by the givenTemporalType. -
setParameter
@Deprecated(since="7") @Nonnull CommonQueryContract setParameter(@Nonnull String parameter, @Nullable Calendar value, @Nonnull TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecated- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameter
@Deprecated(since="7") @Nonnull CommonQueryContract setParameter(@Nonnull String parameter, @Nullable Date value, @Nonnull TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecated- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameter
@Deprecated(since="7") @Nonnull CommonQueryContract setParameter(int parameter, @Nullable Instant value, @Nonnull TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecatedBind anInstantto an ordinal query parameter using just the portion indicated by the givenTemporalType. -
setParameter
@Deprecated(since="7") @Nonnull CommonQueryContract setParameter(int parameter, @Nullable Date value, @Nonnull TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecated- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameter
@Deprecated(since="7") @Nonnull CommonQueryContract setParameter(int parameter, @Nullable Calendar value, @Nonnull TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecated- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameter
@Deprecated(since="7") @Nonnull CommonQueryContract setParameter(@Nonnull Parameter<Calendar> param, @Nullable Calendar value, @Nonnull TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecated- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameter
@Deprecated(since="7") @Nonnull CommonQueryContract setParameter(@Nonnull Parameter<Date> param, @Nullable Date value, @Nonnull TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecated- Specified by:
setParameterin interfaceQuery- See Also:
-
TemporalTypeis deprecated