Top Description Methods
org.hibernate

public Interface Query

Known Direct Subinterfaces
org.hibernate.SQLQuery
Known Direct Implementers
org.hibernate.impl.AbstractQueryImpl
Imports
java.io.Serializable, java.math.BigDecimal, .BigInteger, java.util.Calendar, .Collection, .Date, .Iterator, .List, .Locale, .Map, org.hibernate.transform.ResultTransformer, org.hibernate.type.Type

An object-oriented representation of a Hibernate query. A Query instance is obtained by calling Session.createQuery(). This interface exposes some extra functionality beyond that provided by Session.iterate() and Session.find():
Named query parameters are tokens of the form :name in the query string. A value is bound to the integer parameter :foo by calling

setParameter("foo", foo, Hibernate.INTEGER);

for example. A name may appear multiple times in the query string.

JDBC-style ? parameters are also supported. To bind a value to a JDBC-style parameter use a set method that accepts an int positional argument (numbered from zero, contrary to JDBC).

You may not mix and match JDBC-style parameters and named parameters in the same query.

Queries are executed by calling list(), scroll() or iterate(). A query may be re-executed by subsequent invocations. Its lifespan is, however, bounded by the lifespan of the Session that created it.

Implementors are not intended to be threadsafe.
Author
Gavin King
See Also
org.hibernate.Session#createQuery(java.lang.String), org.hibernate.ScrollableResults

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

The number of entities updated or deleted.
executeUpdate
()

Execute the update or delete statement.

public String[]

Returns:

the parameter names, in no particular order
getNamedParameters
()

Return the names of all named parameters of the query.

public String

Returns:

the query string
getQueryString
()

Get the query string.

public String[]

Returns:

an array of aliases as strings
getReturnAliases
()

Return the HQL select clause aliases (if any)

public Type[]

Returns:

an array of types
getReturnTypes
()

Return the Hibernate types of the query result set.

public boolean

Returns:

true, entities and proxies loaded by the query will be put in read-only mode false, entities and proxies loaded by the query will be put in modifiable mode
isReadOnly
()

Should entities and proxies loaded by this Query be put in read-only mode?

public Iterator<E>

Returns:

the result iterator
iterate
()

Return the query results as an Iterator.

public List<E>

Returns:

the result list
list
()

Return the query results as a List.

public ScrollableResults

Returns:

the result iterator
scroll
()

Return the query results as ScrollableResults.

public ScrollableResults

Returns:

the result iterator
scroll
(ScrollMode scrollMode)

Return the query results as ScrollableResults.

public Query
setBigDecimal(int position, BigDecimal number)

public Query
public Query
setBigInteger(int position, BigInteger number)

public Query
public Query
setBinary(int position, byte[] val)

public Query
setBinary(String name, byte[] val)

public Query
setBoolean(int position, boolean val)

public Query
setBoolean(String name, boolean val)

public Query
setByte(int position, byte val)

public Query
setByte(String name, byte val)

public Query
setCacheable(boolean
Should the query results be cacheable?
cacheable
)

Enable caching of this query result set.

public Query
setCacheMode(CacheMode cacheMode)

Override the current session cache mode, just for this query.

public Query
setCacheRegion(String
the name of a query cache region, or null for the default query cache
cacheRegion
)

Set the name of the cache region.

public Query
setCalendar(int position, Calendar calendar)

public Query
setCalendar(String name, Calendar calendar)

public Query
setCalendarDate(int position, Calendar calendar)

public Query
public Query
setCharacter(int position, char val)

public Query
setCharacter(String name, char val)

public Query
setComment(String
a human-readable string
comment
)

Add a comment to the generated SQL.

public Query
setDate(int position, Date date)

public Query
setDate(String name, Date date)

public Query
setDouble(int position, double val)

public Query
setDouble(String name, double val)

public Query
setEntity(int
the position of the parameter in the query string, numbered from 0.
position
,
Object
a non-null instance of a persistent class
val
)

Bind an instance of a mapped persistent class to a JDBC-style query parameter.

public Query
setEntity(String
the name of the parameter
name
,
Object
a non-null instance of a persistent class
val
)

Bind an instance of a mapped persistent class to a named query parameter.

public Query
setFetchSize(int
the fetch size
fetchSize
)

Set a fetch size for the underlying JDBC query.

public Query
setFirstResult(int
a row number, numbered from 0
firstResult
)

Set the first row to retrieve.

public Query
setFloat(int position, float val)

public Query
setFloat(String name, float val)

public Query
setFlushMode(FlushMode flushMode)

Override the current session flush mode, just for this query.

public Query
setInteger(int position, int val)

public Query
setInteger(String name, int val)

public Query
setLocale(int position, Locale locale)

public Query
setLocale(String name, Locale locale)

public Query
setLockMode(String
a query alias, or this for a collection filter
alias
,
LockMode lockMode)

Set the lockmode for the objects idententified by the given alias that appears in the FROM clause.

public Query
setLockOptions(LockOptions lockOptions)

Set the lock options for the objects idententified by the given alias that appears in the FROM clause.

public Query
setLong(int position, long val)

public Query
setLong(String name, long val)

public Query
setMaxResults(int
the maximum number of rows
maxResults
)

Set the maximum number of rows to retrieve.

public Query
setParameter(int
the position of the parameter in the query string, numbered from 0.
position
,
Object
the possibly-null parameter value
val
,
Type
the Hibernate type
type
)

Bind a value to a JDBC-style query parameter.

public Query
setParameter(String
the name of the parameter
name
,
Object
the possibly-null parameter value
val
,
Type
the Hibernate type
type
)

Bind a value to a named query parameter.

public Query
setParameter(int
the position of the parameter in the query string, numbered from 0.
position
,
Object
the non-null parameter value
val
)

Bind a value to a JDBC-style query parameter.

public Query
setParameter(String
the name of the parameter
name
,
Object
the non-null parameter value
val
)

Bind a value to a named query parameter.

public Query
setParameterList(String
the name of the parameter
name
,
Collection<E>
a collection of values to list
vals
,
Type
the Hibernate type of the values
type
)

Bind multiple values to a named query parameter.

public Query
setParameterList(String
the name of the parameter
name
,
Collection<E>
a collection of values to list
vals
)

Bind multiple values to a named query parameter.

public Query
setParameterList(String
the name of the parameter
name
,
Object[]
a collection of values to list
vals
,
Type
the Hibernate type of the values
type
)

Bind multiple values to a named query parameter.

public Query
setParameterList(String
the name of the parameter
name
,
Object[]
a collection of values to list
vals
)

Bind multiple values to a named query parameter.

public Query
setParameters(Object[] values, Type[] types)

Bind values and types to positional parameters.

public Query
setProperties(Object
any JavaBean or POJO
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 hueristics.

public Query
setProperties(Map<K, V>
a java.util.Map
bean
)

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 hueristics.

public Query
setReadOnly(boolean
true, entities and proxies loaded by the query will be put in read-only mode false, entities and proxies loaded by the query will be put in modifiable mode
readOnly
)

Set the read-only/modifiable mode for entities and proxies loaded by this Query.

public Query

Returns:

this (for method chaining)
setResultTransformer
(ResultTransformer
The transformer to apply
transformer
)

Set a strategy for handling the query results.

public Query
setSerializable(int position, Serializable val)

public Query
public Query
setShort(int position, short val)

public Query
setShort(String name, short val)

public Query
setString(int position, String val)

public Query
setString(String name, String val)

public Query
setText(int position, String val)

public Query
setText(String name, String val)

public Query
setTime(int position, Date date)

public Query
setTime(String name, Date date)

public Query
setTimeout(int
the timeout in seconds
timeout
)

Set a timeout for the underlying JDBC query.

public Query
setTimestamp(int position, Date date)

public Query
setTimestamp(String name, Date date)

public Object

Returns:

the single result or null
uniqueResult
()

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Method Detail

executeUpdateback to summary
public int executeUpdate() throws HibernateException

Execute the update or delete statement.

The semantics are compliant with the ejb3 Query.executeUpdate() method.
Returns:int

The number of entities updated or deleted.

getNamedParametersback to summary
public String[] getNamedParameters() throws HibernateException

Return the names of all named parameters of the query.

Returns:String[]

the parameter names, in no particular order

getQueryStringback to summary
public String getQueryString()

Get the query string.

Returns:String

the query string

getReturnAliasesback to summary
public String[] getReturnAliases() throws HibernateException

Return the HQL select clause aliases (if any)

Returns:String[]

an array of aliases as strings

getReturnTypesback to summary
public Type[] getReturnTypes() throws HibernateException

Return the Hibernate types of the query result set.

Returns:Type[]

an array of types

isReadOnlyback to summary
public boolean isReadOnly()

Should entities and proxies loaded by this Query be put in read-only mode? If the read-only/modifiable setting was not initialized, then the default read-only/modifiable setting for the persistence context is returned instead.

Returns:boolean

true, entities and proxies loaded by the query will be put in read-only mode false, entities and proxies loaded by the query will be put in modifiable mode

See Also
Query#setReadOnly(boolean), The read-only/modifiable setting has no impact on entities/proxies returned by the query that existed in the session before the query was executed.
iterateback to summary
public Iterator<E> iterate() throws HibernateException

Return the query results as an Iterator. If the query contains multiple results pre row, the results are returned in an instance of Object[].

Entities returned as results are initialized on demand. The first SQL query returns identifiers only.

Returns:Iterator<E>

the result iterator

listback to summary
public List<E> list() throws HibernateException

Return the query results as a List. If the query contains multiple results pre row, the results are returned in an instance of Object[].

Returns:List<E>

the result list

scrollback to summary
public ScrollableResults scroll() throws HibernateException

Return the query results as ScrollableResults. The scrollability of the returned results depends upon JDBC driver support for scrollable ResultSets.

Returns:ScrollableResults

the result iterator

See Also
ScrollableResults
scrollback to summary
public ScrollableResults scroll(ScrollMode scrollMode) throws HibernateException

Return the query results as ScrollableResults. The scrollability of the returned results depends upon JDBC driver support for scrollable ResultSets.

Returns:ScrollableResults

the result iterator

See Also
ScrollableResults, ScrollMode
setBigDecimalback to summary
public Query setBigDecimal(int position, BigDecimal number)
setBigDecimalback to summary
public Query setBigDecimal(String name, BigDecimal number)
setBigIntegerback to summary
public Query setBigInteger(int position, BigInteger number)
setBigIntegerback to summary
public Query setBigInteger(String name, BigInteger number)
setBinaryback to summary
public Query setBinary(int position, byte[] val)
setBinaryback to summary
public Query setBinary(String name, byte[] val)
setBooleanback to summary
public Query setBoolean(int position, boolean val)
setBooleanback to summary
public Query setBoolean(String name, boolean val)
setByteback to summary
public Query setByte(int position, byte val)
setByteback to summary
public Query setByte(String name, byte val)
setCacheableback to summary
public Query setCacheable(boolean cacheable)

Enable caching of this query result set.

Parameters
cacheable:boolean

Should the query results be cacheable?

setCacheModeback to summary
public Query setCacheMode(CacheMode cacheMode)

Override the current session cache mode, just for this query.

See Also
org.hibernate.CacheMode
setCacheRegionback to summary
public Query setCacheRegion(String cacheRegion)

Set the name of the cache region.

Parameters
cacheRegion:String

the name of a query cache region, or null for the default query cache

setCalendarback to summary
public Query setCalendar(int position, Calendar calendar)
setCalendarback to summary
public Query setCalendar(String name, Calendar calendar)
setCalendarDateback to summary
public Query setCalendarDate(int position, Calendar calendar)
setCalendarDateback to summary
public Query setCalendarDate(String name, Calendar calendar)
setCharacterback to summary
public Query setCharacter(int position, char val)
setCharacterback to summary
public Query setCharacter(String name, char val)
setCommentback to summary
public Query setComment(String comment)

Add a comment to the generated SQL.

Parameters
comment:String

a human-readable string

setDateback to summary
public Query setDate(int position, Date date)
setDateback to summary
public Query setDate(String name, Date date)
setDoubleback to summary
public Query setDouble(int position, double val)
setDoubleback to summary
public Query setDouble(String name, double val)
setEntityback to summary
public Query setEntity(int position, Object val)

Bind an instance of a mapped persistent class to a JDBC-style query parameter.

Parameters
position:int

the position of the parameter in the query string, numbered from 0.

val:Object

a non-null instance of a persistent class

setEntityback to summary
public Query setEntity(String name, Object val)

Bind an instance of a mapped persistent class to a named query parameter.

Parameters
name:String

the name of the parameter

val:Object

a non-null instance of a persistent class

setFetchSizeback to summary
public Query setFetchSize(int fetchSize)

Set a fetch size for the underlying JDBC query.

Parameters
fetchSize:int

the fetch size

setFirstResultback to summary
public Query setFirstResult(int firstResult)

Set the first row to retrieve. If not set, rows will be retrieved beginnning from row 0.

Parameters
firstResult:int

a row number, numbered from 0

setFloatback to summary
public Query setFloat(int position, float val)
setFloatback to summary
public Query setFloat(String name, float val)
setFlushModeback to summary
public Query setFlushMode(FlushMode flushMode)

Override the current session flush mode, just for this query.

See Also
org.hibernate.FlushMode
setIntegerback to summary
public Query setInteger(int position, int val)
setIntegerback to summary
public Query setInteger(String name, int val)
setLocaleback to summary
public Query setLocale(int position, Locale locale)
setLocaleback to summary
public Query setLocale(String name, Locale locale)
setLockModeback to summary
public Query setLockMode(String alias, LockMode lockMode)

Set the lockmode for the objects idententified by the given alias that appears in the FROM clause.

Parameters
alias:String

a query alias, or this for a collection filter

setLockOptionsback to summary
public Query setLockOptions(LockOptions lockOptions)

Set the lock options for the objects idententified by the given alias that appears in the FROM clause.

setLongback to summary
public Query setLong(int position, long val)
setLongback to summary
public Query setLong(String name, long val)
setMaxResultsback to summary
public Query setMaxResults(int maxResults)

Set the maximum number of rows to retrieve. If not set, there is no limit to the number of rows retrieved.

Parameters
maxResults:int

the maximum number of rows

setParameterback to summary
public Query setParameter(int position, Object val, Type type)

Bind a value to a JDBC-style query parameter.

Parameters
position:int

the position of the parameter in the query string, numbered from 0.

val:Object

the possibly-null parameter value

type:Type

the Hibernate type

setParameterback to summary
public Query setParameter(String name, Object val, Type type)

Bind a value to a named query parameter.

Parameters
name:String

the name of the parameter

val:Object

the possibly-null parameter value

type:Type

the Hibernate type

setParameterback to summary
public Query setParameter(int position, Object val) throws HibernateException

Bind a value to a JDBC-style query parameter. The Hibernate type of the parameter is first detected via the usage/position in the query and if not sufficient secondly guessed from the class of the given object.

Parameters
position:int

the position of the parameter in the query string, numbered from 0.

val:Object

the non-null parameter value

Exceptions
HibernateException:
if no type could be determined
setParameterback to summary
public Query setParameter(String name, Object val) throws HibernateException

Bind a value to a named query parameter. The Hibernate type of the parameter is first detected via the usage/position in the query and if not sufficient secondly guessed from the class of the given object.

Parameters
name:String

the name of the parameter

val:Object

the non-null parameter value

Exceptions
HibernateException:
if no type could be determined
setParameterListback to summary
public Query setParameterList(String name, Collection<E> vals, Type type) throws HibernateException

Bind multiple values to a named query parameter. This is useful for binding a list of values to an expression such as foo.bar in (:value_list).

Parameters
name:String

the name of the parameter

vals:Collection<E>

a collection of values to list

type:Type

the Hibernate type of the values

setParameterListback to summary
public Query setParameterList(String name, Collection<E> vals) throws HibernateException

Bind multiple values to a named query parameter. The Hibernate type of the parameter is first detected via the usage/position in the query and if not sufficient secondly guessed from the class of the first object in the collection. This is useful for binding a list of values to an expression such as foo.bar in (:value_list).

Parameters
name:String

the name of the parameter

vals:Collection<E>

a collection of values to list

setParameterListback to summary
public Query setParameterList(String name, Object[] vals, Type type) throws HibernateException

Bind multiple values to a named query parameter. This is useful for binding a list of values to an expression such as foo.bar in (:value_list).

Parameters
name:String

the name of the parameter

vals:Object[]

a collection of values to list

type:Type

the Hibernate type of the values

setParameterListback to summary
public Query setParameterList(String name, Object[] vals) throws HibernateException

Bind multiple values to a named query parameter. The Hibernate type of the parameter is first detected via the usage/position in the query and if not sufficient secondly guessed from the class of the first object in the array. This is useful for binding a list of values to an expression such as foo.bar in (:value_list).

Parameters
name:String

the name of the parameter

vals:Object[]

a collection of values to list

setParametersback to summary
public Query setParameters(Object[] values, Type[] types) throws HibernateException

Bind values and types to positional parameters.

setPropertiesback to summary
public Query setProperties(Object bean) throws HibernateException

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 hueristics.

Parameters
bean:Object

any JavaBean or POJO

setPropertiesback to summary
public Query setProperties(Map<K, V> bean) throws HibernateException

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 hueristics.

Parameters
bean:Map<K, V>

a java.util.Map

setReadOnlyback to summary
public Query setReadOnly(boolean readOnly)

Set the read-only/modifiable mode for entities and proxies loaded by this Query. This setting overrides the default setting for the persistence context.

Parameters
readOnly:boolean

true, entities and proxies loaded by the query will be put in read-only mode false, entities and proxies loaded by the query will be put in modifiable mode

See Also
To set the default read-only/modifiable setting used for entities and proxies that are loaded into the session:, org.hibernate.engine.PersistenceContext#setDefaultReadOnly(boolean), Read-only entities are not dirty-checked and snapshots of persistent state are not maintained. Read-only entities can be modified, but changes are not persisted. When a proxy is initialized, the loaded entity will have the same read-only/modifiable setting as the uninitialized proxy has, regardless of the session's current setting. The read-only/modifiable setting has no impact on entities/proxies returned by the query that existed in the session before the query was executed.
setResultTransformerback to summary
public Query setResultTransformer(ResultTransformer transformer)

Set a strategy for handling the query results. This can be used to change "shape" of the query result.

Parameters
transformer:ResultTransformer

The transformer to apply

Returns:Query

this (for method chaining)

setSerializableback to summary
public Query setSerializable(int position, Serializable val)
setSerializableback to summary
public Query setSerializable(String name, Serializable val)
setShortback to summary
public Query setShort(int position, short val)
setShortback to summary
public Query setShort(String name, short val)
setStringback to summary
public Query setString(int position, String val)
setStringback to summary
public Query setString(String name, String val)
setTextback to summary
public Query setText(int position, String val)
setTextback to summary
public Query setText(String name, String val)
setTimeback to summary
public Query setTime(int position, Date date)
setTimeback to summary
public Query setTime(String name, Date date)
setTimeoutback to summary
public Query setTimeout(int timeout)

Set a timeout for the underlying JDBC query.

Parameters
timeout:int

the timeout in seconds

setTimestampback to summary
public Query setTimestamp(int position, Date date)
setTimestampback to summary
public Query setTimestamp(String name, Date date)
uniqueResultback to summary
public Object uniqueResult() throws HibernateException

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Returns:Object

the single result or null

Exceptions
NonUniqueResultException:
if there is more than one matching result