Interface CommonQueryContract
- All Superinterfaces:
Query
- All Known Subinterfaces:
CommonQueryContractImplementor, 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.Casts this query as a mutation query.Casts this query as aSelectionQuery.<X> SelectionQuery<X> asSelectionQuery(EntityGraph<X> entityGraph) Casts this query as aSelectionQuerywith the given result graph.<X> SelectionQuery<X> asSelectionQuery(EntityGraph<X> entityGraph, GraphSemantic graphSemantic) Overload of Query.withEntityGraph(EntityGraph) allowing a specific semantic (load/fetch) for the graph.<R> SelectionQuery<R> asSelectionQuery(Class<R> type) Casts this query as aSelectionQuerywith the given result type.Obtain the comment currently associated with this query.TheFlushModein effect for this query.Get theParameterMetadataobject representing the parameters of this query, and providing access to theQueryParameters.TheQueryFlushModein effect for this query.Obtain the query timeout to be applied to the corresponding database 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.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) Apply a timeout to the corresponding database query.setTimeout(Timeout timeout) Apply a timeout to the corresponding database query.setTimeout(Integer timeout) Apply a timeout to the corresponding database query.Methods inherited from interface Query
asStatement, executeUpdate, getCacheRetrieveMode, getCacheStoreMode, getFirstResult, getFlushMode, getHints, getLockMode, getMaxResults, getParameter, getParameter, getParameter, getParameter, getParameters, getParameterValue, getParameterValue, getParameterValue, getResultList, getResultStream, getSingleResult, getSingleResultOrNull, isBound, ofType, setCacheRetrieveMode, setCacheStoreMode, setFirstResult, setFlushMode, setLockMode, setMaxResults, unwrap, withEntityGraph
-
Method Details
-
getSession
SharedSessionContract getSession()- Returns:
- The producer of this query
-
getQueryFlushMode
QueryFlushMode getQueryFlushMode()TheQueryFlushModein effect for this query.By default, this is
QueryFlushMode.DEFAULT, and theFlushModeof the owningSessiondetermines whether it is flushed.- Since:
- 7.0
- See Also:
-
setQueryFlushMode
Set theQueryFlushModeto use for this query.- Since:
- 7.0
- See Also:
-
getEffectiveFlushMode
-
getComment
String 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
Integer getTimeout()Obtain the query timeout to be applied to the corresponding database query.See Timeouts for discussion of "magic values".
- Specified by:
getTimeoutin interfaceQuery- See Also:
- API Note:
- As this method is inherited from JPA, the value expected to be in milliseconds.
- Implementation Note:
- This value is eventually passed along to the JDBC statement via
Statement.setQueryTimeout(int).
-
setTimeout
Apply a timeout to the corresponding database query.- See Also:
- API Note:
- As a legacy Hibernate method, this form expects a value in seconds.
-
setTimeout
Apply a timeout to the corresponding database query.- Specified by:
setTimeoutin interfaceQuery- See Also:
- API Note:
- As this method is inherited from JPA, the value expected to be in milliseconds.
-
setTimeout
Apply a timeout to the corresponding database query.- Specified by:
setTimeoutin interfaceQuery
-
setHint
Set a hint. Hints are a JPA-standard way to control provider-specific behavior affecting execution of the query. Clients of 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.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.
-
asSelectionQuery
SelectionQuery<?> asSelectionQuery()Casts this query as aSelectionQuery.- Throws:
IllegalSelectQueryException- If the query is not a select query.
-
asSelectionQuery
Casts this query as aSelectionQuerywith the given result type.- Throws:
IllegalSelectQueryException- If the query is not a select query.IllegalArgumentException- If the giventypeis not compatible with the query's defined result type.
-
asSelectionQuery
Casts this query as aSelectionQuerywith the given result graph.- Throws:
IllegalSelectQueryException- If the query is not a selection query.IllegalArgumentException- Is the given graph result type is not compatible with theQuerytype parameter.
-
asSelectionQuery
Overload of Query.withEntityGraph(EntityGraph) allowing a specific semantic (load/fetch) for the graph.- Parameters:
entityGraph- The entity graph.graphSemantic- The load/fetch semantic.- Returns:
- The cast/converted query.
- See Also:
-
asMutationQuery
MutationQuery asMutationQuery()Casts this query as a mutation query.- Throws:
IllegalMutationQueryException- If the query is not a mutation query.
-
getParameterMetadata
ParameterMetadata getParameterMetadata()Get theParameterMetadataobject representing the parameters of this query, and providing access to theQueryParameters.- Since:
- 7.0
-
setParameter
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:
-
setParameter
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
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
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
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
Bind the given argument to a named query parameter using the givenType.- Specified by:
setParameterin interfaceQuery
-
setParameter
Bind the given argument to an ordinal query parameter using the givenType.- Specified by:
setParameterin interfaceQuery
-
setConvertedParameter
<P> CommonQueryContract setConvertedParameter(String name, P value, Class<? extends AttributeConverter<P, ?>> converter) - Specified by:
setConvertedParameterin interfaceQuery- See Also:
-
setConvertedParameter
<P> CommonQueryContract setConvertedParameter(int position, P value, Class<? extends AttributeConverter<P, ?>> converter) - Specified by:
setConvertedParameterin interfaceQuery- See Also:
-
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- See Also:
- API Note:
- This is used for binding a list of values to an expression
such as
entity.field in (:values).
-
setParameterList
<P> CommonQueryContract 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).- 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
<P> CommonQueryContract setParameterList(String parameter, Collection<? extends P> values, 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
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
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
<P> CommonQueryContract setParameterList(int parameter, 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
<P> CommonQueryContract setParameterList(int parameter, 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
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
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
<P> CommonQueryContract setParameterList(QueryParameter<P> parameter, 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
<P> CommonQueryContract 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. 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
<P> CommonQueryContract 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.- 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 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
<P> CommonQueryContract 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. 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
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") CommonQueryContract setParameter(String parameter, Instant value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecatedBind anInstantto the named query parameter using just the portion indicated by the givenTemporalType. -
setParameter
@Deprecated(since="7") CommonQueryContract setParameter(String parameter, Calendar value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecated- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameter
@Deprecated(since="7") CommonQueryContract setParameter(String parameter, Date value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecated- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameter
@Deprecated(since="7") CommonQueryContract setParameter(int parameter, Instant value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecatedBind anInstantto an ordinal query parameter using just the portion indicated by the givenTemporalType. -
setParameter
@Deprecated(since="7") CommonQueryContract setParameter(int parameter, Date value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecated- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameter
@Deprecated(since="7") CommonQueryContract setParameter(int parameter, Calendar value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecated- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameter
@Deprecated(since="7") CommonQueryContract setParameter(Parameter<Calendar> param, Calendar value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecated- Specified by:
setParameterin interfaceQuery- See Also:
-
setParameter
@Deprecated(since="7") CommonQueryContract setParameter(Parameter<Date> param, Date value, TemporalType temporalType) Deprecated.sinceTemporalTypeis deprecated- Specified by:
setParameterin interfaceQuery- See Also:
-
TemporalTypeis deprecated