Top Description Methods
org.hibernate.query

public Interface Query<R>

extends SelectionQuery<R>, MutationQuery, TypedQuery<R>
Known Direct Subinterfaces
org.hibernate.query.spi.QueryImplementor, org.hibernate.query.NativeQuery
Annotations
@Incubating
Type Parameters
<R>
The result type, for typed queries, or Object for untyped queries
Imports
java.time.Instant, java.util.Calendar, .Collection, .Date, .List, .Map, .Optional, java.util.stream.Stream, jakarta.persistence.EntityGraph, .CacheRetrieveMode, .CacheStoreMode, .FlushModeType, .LockModeType, .Parameter, .TemporalType, .TypedQuery, org.hibernate.CacheMode, .FlushMode, .Incubating, .LockMode, .LockOptions, .NonUniqueResultException, .ScrollMode, .ScrollableResults, .SharedSessionContract, org.hibernate.dialect.Dialect, org.hibernate.graph.GraphSemantic, .RootGraph, org.hibernate.query.spi.QueryOptions, org.hibernate.transform.ResultTransformer

Within the context of an active session, an instance of this type represents an executable query, either:

The subtype NativeQuery represents a query written in native SQL.

This type simply mixes the TypedQuery interface defined by JPA with SelectionQuery and MutationQuery. Unfortunately, JPA does not distinguish between selection queries and mutation queries, so we lose that distinction here. However, every Query may logically be classified as one or the other.

A Query may be obtained from the org.hibernate.Session by calling:

A Query controls how a query is executed, and allows arguments to be bound to its parameters.

Note that this interface offers no real advantages over SelectionQuery except for compatibility with the JPA-defined TypedQuery interface.

Authors
Gavin King, Steve Ebersole
See Also
QueryProducer

Method Summary

Modifier and TypeMethod and Description
public Query<R>
addQueryHint(String
The database specific query hint to add.
hint
)

Add a database query hint to the SQL query.

public default Query<R>
applyFetchGraph(RootGraph<J> graph)

Apply the given graph using fetch semantics.

public Query<R>
applyGraph(RootGraph<J>
The graph to apply.
graph
,
GraphSemantic
The semantic to use when applying the graph
semantic
)

Apply the given graph using the given semantic

public default Query<R>
applyLoadGraph(RootGraph<J> graph)

Apply the given graph using load semantics.

public Query<R>
disableFetchProfile(String
the name of the fetch profile to be disabled
profileName
)

Redeclares org.hibernate.query.SelectionQuery.disableFetchProfile.

Disable the fetch profile with the given name in this session.
public Query<R>
enableFetchProfile(String
the name of the fetch profile to be enabled
profileName
)

Redeclares org.hibernate.query.SelectionQuery.enableFetchProfile.

Enable the fetch profile for this query.
public int

Returns:

the number of affected entity instances (may differ from the number of affected rows)
executeUpdate
()

Redeclares org.hibernate.query.MutationQuery.executeUpdate, jakarta.persistence.Query.executeUpdate.

Execute an insert, update, or delete statement, and return the number of affected entities.
public String

Returns:

The comment.
getComment
()

Redeclares org.hibernate.query.CommonQueryContract.getComment.

Obtain the comment currently associated with this query.
public LockOptions

Returns:

The LockOptions currently in effect
getLockOptions
()

Redeclares org.hibernate.query.SelectionQuery.getLockOptions.

Obtains the LockOptions in effect for this query.
public ParameterMetadata

Returns:

information about query parameters.
getParameterMetadata
()

Access to information about query parameters.

public QueryOptions

Returns:

Return the encapsulation of this query's options.
getQueryOptions
()

Get the execution options for this Query.

public String
getQueryString()

The query as a string, or null in the case of a criteria query.

public default List<R>

Returns:

the results as a list
getResultList
()

Overrides default org.hibernate.query.SelectionQuery.getResultList.

Implements jakarta.persistence.TypedQuery.getResultList.

Execute the query and return the query results as a List.
public default Stream<R>

Returns:

The results as a Stream
getResultStream
()

Overrides default org.hibernate.query.SelectionQuery.getResultStream, jakarta.persistence.TypedQuery.getResultStream.

Execute the query and return the query results as a Stream.
public SharedSessionContract

Returns:

The producer of this query
getSession
()

Get the QueryProducer which produced this Query, that is, the org.hibernate.Session or org.hibernate.StatelessSession that was used to create this Query instance.

public R

Returns:

the single result, only if there is exactly one
getSingleResult
()

Redeclares org.hibernate.query.SelectionQuery.getSingleResult, jakarta.persistence.TypedQuery.getSingleResult.

Execute the query and return the single result of the query, throwing an exception if the query returns no results.
public List<R>

Returns:

the result list
list
()

Redeclares org.hibernate.query.SelectionQuery.list.

Execute the query and return the query results as a List.
public ScrollableResults<R>
scroll()

Redeclares org.hibernate.query.SelectionQuery.scroll.

Execute the query and return the results in a scrollable form.
public ScrollableResults<R>
scroll(ScrollMode scrollMode)

Redeclares org.hibernate.query.SelectionQuery.scroll.

Execute the query and return the results in a scrollable form.
public Query<R>
setCacheable(boolean cacheable)

Redeclares org.hibernate.query.SelectionQuery.setCacheable.

Enable/disable second level query (result) caching for this query.
public Query<R>
setCacheMode(CacheMode cacheMode)

Redeclares org.hibernate.query.SelectionQuery.setCacheMode.

Set the current CacheMode in effect for this query.
public Query<R>
setCacheRegion(String cacheRegion)

Redeclares org.hibernate.query.SelectionQuery.setCacheRegion.

Set the name of the cache region where query results should be cached (assuming isCacheable).
public Query<R>
public Query<R>
public Query<R>

Returns:

this, for method chaining
setComment
(String
The human-readable comment
comment
)

Redeclares org.hibernate.query.SelectionQuery.setComment, org.hibernate.query.MutationQuery.setComment.

Set the comment for this query.
public Query<R>
public Query<R>
setFetchSize(int
the fetch size hint
fetchSize
)

Redeclares org.hibernate.query.SelectionQuery.setFetchSize.

Sets a JDBC fetch size hint for the query.
public Query<R>
setFirstResult(int
position of the first result, numbered from 0
startPosition
)

Redeclares org.hibernate.query.SelectionQuery.setFirstResult, jakarta.persistence.TypedQuery.setFirstResult.

Set the first row position to return from the query results.
public Query<R>
public Query<R>
public Query<R>
setHint(String
name of property or hint
hintName
,
Object
value for the property or hint
value
)

Redeclares org.hibernate.query.SelectionQuery.setHint, org.hibernate.query.MutationQuery.setHint, jakarta.persistence.TypedQuery.setHint.

Set a query property or hint.
public Query<R>

Returns:

this, for method chaining
setLockMode
(String
A query alias
alias
,
LockMode
The lock mode to apply
lockMode
)

Redeclares org.hibernate.query.SelectionQuery.setLockMode.

Set the LockMode to use for particular alias defined in the FROM clause of the query.
public Query<R>
public Query<R>

Returns:

this, for method chaining
setLockOptions
(LockOptions
The lock options to apply to the query.
lockOptions
)

Apply the given lock options to this query.

public Query<R>
setMaxResults(int
maximum number of results to retrieve
maxResult
)

Redeclares org.hibernate.query.SelectionQuery.setMaxResults, jakarta.persistence.TypedQuery.setMaxResults.

Set the max number of rows requested for the query results.
public Query<R>
setOrder(List<Order<? super R>>
one or more instances of Order
orderList
)

Redeclares org.hibernate.query.SelectionQuery.setOrder.

If the result type of this query is an entity class, add one or more rules for ordering the query results.
public Query<R>
setOrder(Order<? super R>
an instance of Order
order
)

Redeclares org.hibernate.query.SelectionQuery.setOrder.

If the result type of this query is an entity class, add a rule for ordering the query results.
public default Query<R>
setPage(Page page)

Implements org.hibernate.query.SelectionQuery.setPage.

Set the page of results to return.
public Query<R>
setParameter(String
parameter name
parameter
,
Object
parameter value
argument
)

Redeclares org.hibernate.query.SelectionQuery.setParameter, org.hibernate.query.MutationQuery.setParameter, jakarta.persistence.TypedQuery.setParameter.

Bind the given argument to a named query parameter.
public <P> Query<R>
setParameter(String parameter, P argument, Class<P> type)

Redeclares org.hibernate.query.SelectionQuery.setParameter, org.hibernate.query.MutationQuery.setParameter.

Bind the given argument to a named query parameter using the given Class reference to attempt to determine the BindableType to use.
public <P> Query<R>
setParameter(String parameter, P argument, BindableType<P> type)

Redeclares org.hibernate.query.SelectionQuery.setParameter, org.hibernate.query.MutationQuery.setParameter.

Bind the given argument to a named query parameter using the given BindableType.
public Query<R>
setParameter(String parameter, Instant argument, TemporalType temporalType)

Redeclares org.hibernate.query.SelectionQuery.setParameter, org.hibernate.query.MutationQuery.setParameter.

Bind an Instant value to the named query parameter using just the portion indicated by the given TemporalType.
public Query<R>
public Query<R>
public Query<R>
setParameter(int
position
parameter
,
Object
parameter value
argument
)

Redeclares org.hibernate.query.SelectionQuery.setParameter, org.hibernate.query.MutationQuery.setParameter, jakarta.persistence.TypedQuery.setParameter.

Bind the given argument to an ordinal query parameter.
public <P> Query<R>
setParameter(int parameter, P argument, Class<P> type)

Redeclares org.hibernate.query.SelectionQuery.setParameter, org.hibernate.query.MutationQuery.setParameter.

Bind the given argument to an ordinal query parameter using the given Class reference to attempt to determine the BindableType to use.
public <P> Query<R>
setParameter(int parameter, P argument, BindableType<P> type)

Redeclares org.hibernate.query.SelectionQuery.setParameter, org.hibernate.query.MutationQuery.setParameter.

Bind the given argument to an ordinal query parameter using the given BindableType.
public Query<R>
setParameter(int parameter, Instant argument, TemporalType temporalType)

Redeclares org.hibernate.query.SelectionQuery.setParameter, org.hibernate.query.MutationQuery.setParameter.

Bind an Instant value to the ordinal query parameter using just the portion indicated by the given TemporalType.
public Query<R>
public Query<R>
public <T> Query<R>

Returns:

this, for method chaining
setParameter
(QueryParameter<T>
the query parameter memento
parameter
,
T
the argument, which might be null
argument
)

Redeclares org.hibernate.query.SelectionQuery.setParameter, org.hibernate.query.MutationQuery.setParameter.

Bind an argument to the query parameter represented by the given QueryParameter.
public <P> Query<R>

Returns:

this, for method chaining
setParameter
(QueryParameter<P>
the query parameter memento
parameter
,
P
the argument, which might be null
argument
,
Class<P>
a BindableType representing the type of the parameter
type
)

Redeclares org.hibernate.query.SelectionQuery.setParameter, org.hibernate.query.MutationQuery.setParameter.

Bind an argument to the query parameter represented by the given QueryParameter using the given Class reference to attempt to determine the BindableType to use.
public <P> Query<R>

Returns:

this, for method chaining
setParameter
(QueryParameter<P>
the query parameter memento
parameter
,
P
the argument, which might be null
argument
,
BindableType<P>
an BindableType representing the type of the parameter
type
)

Redeclares org.hibernate.query.SelectionQuery.setParameter, org.hibernate.query.MutationQuery.setParameter.

Bind an argument to the query parameter represented by the given QueryParameter using the given BindableType.
public <T> Query<R>
public Query<R>
public Query<R>
public Query<R>

Returns:

this, for method chaining
setParameterList
(String parameter, Collection<E> arguments)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to a named query parameter.
public <P> Query<R>

Returns:

this, for method chaining
setParameterList
(String parameter, Collection<? extends P> arguments, Class<P> javaType)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to a named query parameter using the given Class reference to attempt to determine the BindableType to use.
public <P> Query<R>

Returns:

this, for method chaining
setParameterList
(String parameter, Collection<? extends P> arguments, BindableType<P> type)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to a named query parameter using the given BindableType.
public Query<R>

Returns:

this, for method chaining
setParameterList
(String parameter, Object[] values)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to a named query parameter.
public <P> Query<R>

Returns:

this, for method chaining
setParameterList
(String parameter, P[] arguments, Class<P> javaType)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to a named query parameter using the given Class reference to attempt to determine the BindableType to use.
public <P> Query<R>

Returns:

this, for method chaining
setParameterList
(String parameter, P[] arguments, BindableType<P> type)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to a named query parameter using the given BindableType.
public Query<R>

Returns:

this, for method chaining
setParameterList
(int parameter, Collection<E> arguments)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to an ordinal query parameter.
public <P> Query<R>

Returns:

this, for method chaining
setParameterList
(int parameter, Collection<? extends P> arguments, Class<P> javaType)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to an ordinal query parameter using the given Class reference to attempt to determine the BindableType to use.
public <P> Query<R>

Returns:

this, for method chaining
setParameterList
(int parameter, Collection<? extends P> arguments, BindableType<P> type)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to an ordinal query parameter using the given BindableType.
public Query<R>

Returns:

this, for method chaining
setParameterList
(int parameter, Object[] arguments)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to an ordinal query parameter.
public <P> Query<R>

Returns:

this, for method chaining
setParameterList
(int parameter, P[] arguments, Class<P> javaType)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to an ordinal query parameter using the given Class reference to attempt to determine the BindableType to use.
public <P> Query<R>

Returns:

this, for method chaining
setParameterList
(int parameter, P[] arguments, BindableType<P> type)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to an ordinal query parameter using the given BindableType.
public <P> Query<R>

Returns:

this, for method chaining
setParameterList
(QueryParameter<P>
the parameter memento
parameter
,
Collection<? extends P>
a collection of arguments
arguments
)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to the query parameter represented by the given QueryParameter.
public <P> Query<R>

Returns:

this, for method chaining
setParameterList
(QueryParameter<P> parameter, Collection<? extends P> arguments, Class<P> javaType)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to the query parameter represented by the given QueryParameter using the given Class reference to attempt to determine the BindableType to use.
public <P> Query<R>

Returns:

this, for method chaining
setParameterList
(QueryParameter<P> parameter, Collection<? extends P> arguments, BindableType<P> type)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to the query parameter represented by the given QueryParameter, inferring the BindableType.
public <P> Query<R>

Returns:

this, for method chaining
setParameterList
(QueryParameter<P>
the parameter memento
parameter
,
P[]
a collection of arguments
arguments
)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to the query parameter represented by the given QueryParameter.
public <P> Query<R>

Returns:

this, for method chaining
setParameterList
(QueryParameter<P> parameter, P[] arguments, Class<P> javaType)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to the query parameter represented by the given QueryParameter using the given Class reference to attempt to determine the BindableType to use.
public <P> Query<R>

Returns:

this, for method chaining
setParameterList
(QueryParameter<P> parameter, P[] arguments, BindableType<P> type)

Redeclares org.hibernate.query.SelectionQuery.setParameterList, org.hibernate.query.MutationQuery.setParameterList.

Bind multiple arguments to the query parameter represented by the given QueryParameter, inferring the BindableType.
public Query<R>

Returns:

this, for method chaining
setProperties
(Object
any JavaBean or POJO
bean
)

Redeclares org.hibernate.query.SelectionQuery.setProperties, org.hibernate.query.MutationQuery.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.
public Query<R>

Returns:

this, for method chaining
setProperties
(Map<K, V>
a Map of names to arguments
bean
)

Redeclares org.hibernate.query.SelectionQuery.setProperties, org.hibernate.query.MutationQuery.setProperties.

Bind the values of the given Map for each named parameters of the query, matching key names with parameter names and mapping value types to Hibernate types using heuristics.
public Query<R>
setReadOnly(boolean
true indicates that entities and proxies loaded by the query are to be put in read-only mode; false indicates that entities and proxies loaded by the query will be put in modifiable mode
readOnly
)

Redeclares org.hibernate.query.SelectionQuery.setReadOnly.

Set the read-only/modifiable mode for entities and proxies loaded by this Query.
public Query<R>
public default <T> Query<T>
public Query<R>
setTimeout(int
the timeout in seconds
timeout
)

Redeclares org.hibernate.query.SelectionQuery.setTimeout, org.hibernate.query.MutationQuery.setTimeout.

Set the query timeout in seconds.
public <T> Query<T>
public default Stream<R>

Returns:

The results as a Stream
stream
()

Overrides default org.hibernate.query.SelectionQuery.stream.

Execute the query and return the query results as a Stream.
public R

Returns:

the single result or null if there is no result to return
uniqueResult
()

Redeclares org.hibernate.query.SelectionQuery.uniqueResult.

Execute the query and return the single result of the query, or null if the query returns no results.
public Optional<R>

Returns:

the single result as an Optional
uniqueResultOptional
()

Redeclares org.hibernate.query.SelectionQuery.uniqueResultOptional.

Execute the query and return the single result of the query as an instance of Optional.
Inherited from org.hibernate.query.SelectionQuery:
getCacheModegetCacheRegiongetCacheRetrieveModegetCacheStoreModegetFetchSizegetFirstResultgetHibernateLockModegetLockModegetMaxResultsgetSingleResultOrNullisCacheableisQueryPlanCacheableisReadOnlysetAliasSpecificLockModesetFollowOnLockingsetHibernateLockModesetQueryPlanCacheable