org.hibernate
Interface Query

All Known Implementing Classes:
org.hibernate.impl.AbstractQueryImpl, QueryImpl, SQLQueryImpl

public interface Query

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:
gperrone
See Also:
Session.createQuery(java.lang.String), ScrollableResults

Method Summary
 int count()
          Return the query results count without extracting it.
 int executeUpdate()
          Execute the update or delete statement.
 java.lang.String[] getNamedParameters()
          Return the names of all named parameters of the query.
 java.lang.String getQueryString()
          Get the query string.
 java.lang.String[] getReturnAliases()
          Return the HQL select clause aliases (if any)
 org.hibernate.type.Type[] getReturnTypes()
          Return the Hibernate types of the query result set.
 java.util.Iterator iterate()
          Return the query results as an Iterator.
 java.util.List list()
          Return the query results as a List.
 org.hibernate.ScrollableResults scroll()
          Return the query results as ScrollableResults.
 org.hibernate.ScrollableResults scroll(org.hibernate.ScrollMode scrollMode)
          Return the query results as ScrollableResults.
 Query setBigDecimal(int position, java.math.BigDecimal number)
           
 Query setBigDecimal(java.lang.String name, java.math.BigDecimal number)
           
 Query setBigInteger(int position, java.math.BigInteger number)
           
 Query setBigInteger(java.lang.String name, java.math.BigInteger number)
           
 Query setBinary(int position, byte[] val)
           
 Query setBinary(java.lang.String name, byte[] val)
           
 Query setBoolean(int position, boolean val)
           
 Query setBoolean(java.lang.String name, boolean val)
           
 Query setByte(int position, byte val)
           
 Query setByte(java.lang.String name, byte val)
           
 Query setCacheable(boolean cacheable)
          Enable caching of this query result set.
 Query setCacheMode(org.hibernate.CacheMode cacheMode)
          Override the current session cache mode, just for this query.
 Query setCacheRegion(java.lang.String cacheRegion)
          Set the name of the cache region.
 Query setCalendar(int position, java.util.Calendar calendar)
           
 Query setCalendar(java.lang.String name, java.util.Calendar calendar)
           
 Query setCalendarDate(int position, java.util.Calendar calendar)
           
 Query setCalendarDate(java.lang.String name, java.util.Calendar calendar)
           
 Query setCharacter(int position, char val)
           
 Query setCharacter(java.lang.String name, char val)
           
 Query setComment(java.lang.String comment)
          Add a comment to the generated SQL.
 Query setDate(int position, java.util.Date date)
           
 Query setDate(java.lang.String name, java.util.Date date)
           
 Query setDouble(int position, double val)
           
 Query setDouble(java.lang.String name, double val)
           
 Query setEntity(int position, java.lang.Object val)
          Bind an instance of a mapped persistent class to a JDBC-style query parameter.
 Query setEntity(java.lang.String name, java.lang.Object val)
          Bind an instance of a mapped persistent class to a named query parameter.
 Query setFetchSize(int fetchSize)
          Set a fetch size for the underlying JDBC query.
 Query setFirstResult(int firstResult)
          Set the first row to retrieve.
 Query setFloat(int position, float val)
           
 Query setFloat(java.lang.String name, float val)
           
 Query setFlushMode(org.hibernate.FlushMode flushMode)
          Override the current session flush mode, just for this query.
 Query setInteger(int position, int val)
           
 Query setInteger(java.lang.String name, int val)
           
 Query setLocale(int position, java.util.Locale locale)
           
 Query setLocale(java.lang.String name, java.util.Locale locale)
           
 Query setLockMode(java.lang.String alias, org.hibernate.LockMode lockMode)
          Set the lockmode for the objects idententified by the given alias that appears in the FROM clause.
 Query setLong(int position, long val)
           
 Query setLong(java.lang.String name, long val)
           
 Query setMaxResults(int maxResults)
          Set the maximum number of rows to retrieve.
 Query setParameter(int position, java.lang.Object val)
          Bind a value to a JDBC-style query parameter.
 Query setParameter(int position, java.lang.Object val, org.hibernate.type.Type type)
          Bind a value to a JDBC-style query parameter.
 Query setParameter(java.lang.String name, java.lang.Object val)
          Bind a value to a named query parameter.
 Query setParameter(java.lang.String name, java.lang.Object val, org.hibernate.type.Type type)
          Bind a value to a named query parameter.
 Query setParameterList(java.lang.String name, java.util.Collection vals)
          Bind multiple values to a named query parameter.
 Query setParameterList(java.lang.String name, java.util.Collection vals, org.hibernate.type.Type type)
          Bind multiple values to a named query parameter.
 Query setParameterList(java.lang.String name, java.lang.Object[] vals)
          Bind multiple values to a named query parameter.
 Query setParameterList(java.lang.String name, java.lang.Object[] vals, org.hibernate.type.Type type)
          Bind multiple values to a named query parameter.
 Query setParameters(java.lang.Object[] values, org.hibernate.type.Type[] types)
          Bind values and types to positional parameters.
 Query setProperties(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.
 Query setProperties(java.lang.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 hueristics.
 Query setReadOnly(boolean readOnly)
          Entities retrieved by this query will be loaded in a read-only mode where Hibernate will never dirty-check them or make changes persistent.
 Query setResultTransformer(org.hibernate.transform.ResultTransformer transformer)
          Set a strategy for handling the query results.
 Query setSerializable(int position, java.io.Serializable val)
           
 Query setSerializable(java.lang.String name, java.io.Serializable val)
           
 Query setShort(int position, short val)
           
 Query setShort(java.lang.String name, short val)
           
 Query setString(int position, java.lang.String val)
           
 Query setString(java.lang.String name, java.lang.String val)
           
 Query setText(int position, java.lang.String val)
           
 Query setText(java.lang.String name, java.lang.String val)
           
 Query setTime(int position, java.util.Date date)
           
 Query setTime(java.lang.String name, java.util.Date date)
           
 Query setTimeout(int timeout)
          Set a timeout for the underlying JDBC query.
 Query setTimestamp(int position, java.util.Date date)
           
 Query setTimestamp(java.lang.String name, java.util.Date date)
           
 java.lang.Object uniqueResult()
          Convenience method to return a single instance that matches the query, or null if the query returns no results.
 

Method Detail

getQueryString

java.lang.String getQueryString()
Get the query string.

Returns:
the query string

getReturnTypes

org.hibernate.type.Type[] getReturnTypes()
                                         throws org.hibernate.HibernateException
Return the Hibernate types of the query result set.

Returns:
an array of types
Throws:
org.hibernate.HibernateException

getReturnAliases

java.lang.String[] getReturnAliases()
                                    throws org.hibernate.HibernateException
Return the HQL select clause aliases (if any)

Returns:
an array of aliases as strings
Throws:
org.hibernate.HibernateException

getNamedParameters

java.lang.String[] getNamedParameters()
                                      throws org.hibernate.HibernateException
Return the names of all named parameters of the query.

Returns:
the parameter names, in no particular order
Throws:
org.hibernate.HibernateException

iterate

java.util.Iterator iterate()
                           throws org.hibernate.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:
the result iterator
Throws:
HibernateException

scroll

org.hibernate.ScrollableResults scroll()
                                       throws org.hibernate.HibernateException
Return the query results as ScrollableResults. The scrollability of the returned results depends upon JDBC driver support for scrollable ResultSets.

Returns:
the result iterator
Throws:
HibernateException
See Also:
ScrollableResults

scroll

org.hibernate.ScrollableResults scroll(org.hibernate.ScrollMode scrollMode)
                                       throws org.hibernate.HibernateException
Return the query results as ScrollableResults. The scrollability of the returned results depends upon JDBC driver support for scrollable ResultSets.

Returns:
the result iterator
Throws:
HibernateException
See Also:
ScrollableResults, ScrollMode

list

java.util.List list()
                    throws org.hibernate.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:
the result list
Throws:
HibernateException

count

int count()
          throws org.hibernate.HibernateException
Return the query results count without extracting it. It works executing a select count(*) on the given query criteria filter

Returns:
the query filtered row count
Throws:
HibernateException

uniqueResult

java.lang.Object uniqueResult()
                              throws org.hibernate.HibernateException
Convenience method to return a single instance that matches the query, or null if the query returns no results.

Returns:
the single result or null
Throws:
NonUniqueResultException - if there is more than one matching result
org.hibernate.HibernateException

executeUpdate

int executeUpdate()
                  throws org.hibernate.HibernateException
Execute the update or delete statement.

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

Returns:
The number of entities updated or deleted.
Throws:
HibernateException

setMaxResults

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 - the maximum number of rows

setFirstResult

Query setFirstResult(int firstResult)
Set the first row to retrieve. If not set, rows will be retrieved beginnning from row 0.

Parameters:
firstResult - a row number, numbered from 0

setReadOnly

Query setReadOnly(boolean readOnly)
Entities retrieved by this query will be loaded in a read-only mode where Hibernate will never dirty-check them or make changes persistent.


setCacheable

Query setCacheable(boolean cacheable)
Enable caching of this query result set.

Parameters:
cacheable - Should the query results be cacheable?

setCacheRegion

Query setCacheRegion(java.lang.String cacheRegion)
Set the name of the cache region.

Parameters:
cacheRegion - the name of a query cache region, or null for the default query cache

setTimeout

Query setTimeout(int timeout)
Set a timeout for the underlying JDBC query.

Parameters:
timeout - the timeout in seconds

setFetchSize

Query setFetchSize(int fetchSize)
Set a fetch size for the underlying JDBC query.

Parameters:
fetchSize - the fetch size

setLockMode

Query setLockMode(java.lang.String alias,
                  org.hibernate.LockMode lockMode)
Set the lockmode for the objects idententified by the given alias that appears in the FROM clause.

Parameters:
alias - a query alias, or this for a collection filter

setComment

Query setComment(java.lang.String comment)
Add a comment to the generated SQL.

Parameters:
comment - a human-readable string

setFlushMode

Query setFlushMode(org.hibernate.FlushMode flushMode)
Override the current session flush mode, just for this query.

See Also:
FlushMode

setCacheMode

Query setCacheMode(org.hibernate.CacheMode cacheMode)
Override the current session cache mode, just for this query.

See Also:
CacheMode

setParameter

Query setParameter(int position,
                   java.lang.Object val,
                   org.hibernate.type.Type type)
Bind a value to a JDBC-style query parameter.

Parameters:
position - the position of the parameter in the query string, numbered from 0.
val - the possibly-null parameter value
type - the Hibernate type

setParameter

Query setParameter(java.lang.String name,
                   java.lang.Object val,
                   org.hibernate.type.Type type)
Bind a value to a named query parameter.

Parameters:
name - the name of the parameter
val - the possibly-null parameter value
type - the Hibernate type

setParameter

Query setParameter(int position,
                   java.lang.Object val)
                   throws org.hibernate.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 - the position of the parameter in the query string, numbered from 0.
val - the non-null parameter value
Throws:
org.hibernate.HibernateException - if no type could be determined

setParameter

Query setParameter(java.lang.String name,
                   java.lang.Object val)
                   throws org.hibernate.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 - the name of the parameter
val - the non-null parameter value
Throws:
org.hibernate.HibernateException - if no type could be determined

setParameters

Query setParameters(java.lang.Object[] values,
                    org.hibernate.type.Type[] types)
                    throws org.hibernate.HibernateException
Bind values and types to positional parameters.

Throws:
org.hibernate.HibernateException

setParameterList

Query setParameterList(java.lang.String name,
                       java.util.Collection vals,
                       org.hibernate.type.Type type)
                       throws org.hibernate.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 - the name of the parameter
vals - a collection of values to list
type - the Hibernate type of the values
Throws:
org.hibernate.HibernateException

setParameterList

Query setParameterList(java.lang.String name,
                       java.util.Collection vals)
                       throws org.hibernate.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 - the name of the parameter
vals - a collection of values to list
Throws:
org.hibernate.HibernateException

setParameterList

Query setParameterList(java.lang.String name,
                       java.lang.Object[] vals,
                       org.hibernate.type.Type type)
                       throws org.hibernate.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 - the name of the parameter
vals - a collection of values to list
type - the Hibernate type of the values
Throws:
org.hibernate.HibernateException

setParameterList

Query setParameterList(java.lang.String name,
                       java.lang.Object[] vals)
                       throws org.hibernate.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 - the name of the parameter
vals - a collection of values to list
Throws:
org.hibernate.HibernateException

setProperties

Query setProperties(java.lang.Object bean)
                    throws org.hibernate.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 - any JavaBean or POJO
Throws:
org.hibernate.HibernateException

setProperties

Query setProperties(java.util.Map bean)
                    throws org.hibernate.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 - a java.util.Map
Throws:
org.hibernate.HibernateException

setString

Query setString(int position,
                java.lang.String val)

setCharacter

Query setCharacter(int position,
                   char val)

setBoolean

Query setBoolean(int position,
                 boolean val)

setByte

Query setByte(int position,
              byte val)

setShort

Query setShort(int position,
               short val)

setInteger

Query setInteger(int position,
                 int val)

setLong

Query setLong(int position,
              long val)

setFloat

Query setFloat(int position,
               float val)

setDouble

Query setDouble(int position,
                double val)

setBinary

Query setBinary(int position,
                byte[] val)

setText

Query setText(int position,
              java.lang.String val)

setSerializable

Query setSerializable(int position,
                      java.io.Serializable val)

setLocale

Query setLocale(int position,
                java.util.Locale locale)

setBigDecimal

Query setBigDecimal(int position,
                    java.math.BigDecimal number)

setBigInteger

Query setBigInteger(int position,
                    java.math.BigInteger number)

setDate

Query setDate(int position,
              java.util.Date date)

setTime

Query setTime(int position,
              java.util.Date date)

setTimestamp

Query setTimestamp(int position,
                   java.util.Date date)

setCalendar

Query setCalendar(int position,
                  java.util.Calendar calendar)

setCalendarDate

Query setCalendarDate(int position,
                      java.util.Calendar calendar)

setString

Query setString(java.lang.String name,
                java.lang.String val)

setCharacter

Query setCharacter(java.lang.String name,
                   char val)

setBoolean

Query setBoolean(java.lang.String name,
                 boolean val)

setByte

Query setByte(java.lang.String name,
              byte val)

setShort

Query setShort(java.lang.String name,
               short val)

setInteger

Query setInteger(java.lang.String name,
                 int val)

setLong

Query setLong(java.lang.String name,
              long val)

setFloat

Query setFloat(java.lang.String name,
               float val)

setDouble

Query setDouble(java.lang.String name,
                double val)

setBinary

Query setBinary(java.lang.String name,
                byte[] val)

setText

Query setText(java.lang.String name,
              java.lang.String val)

setSerializable

Query setSerializable(java.lang.String name,
                      java.io.Serializable val)

setLocale

Query setLocale(java.lang.String name,
                java.util.Locale locale)

setBigDecimal

Query setBigDecimal(java.lang.String name,
                    java.math.BigDecimal number)

setBigInteger

Query setBigInteger(java.lang.String name,
                    java.math.BigInteger number)

setDate

Query setDate(java.lang.String name,
              java.util.Date date)

setTime

Query setTime(java.lang.String name,
              java.util.Date date)

setTimestamp

Query setTimestamp(java.lang.String name,
                   java.util.Date date)

setCalendar

Query setCalendar(java.lang.String name,
                  java.util.Calendar calendar)

setCalendarDate

Query setCalendarDate(java.lang.String name,
                      java.util.Calendar calendar)

setEntity

Query setEntity(int position,
                java.lang.Object val)
Bind an instance of a mapped persistent class to a JDBC-style query parameter.

Parameters:
position - the position of the parameter in the query string, numbered from 0.
val - a non-null instance of a persistent class

setEntity

Query setEntity(java.lang.String name,
                java.lang.Object val)
Bind an instance of a mapped persistent class to a named query parameter.

Parameters:
name - the name of the parameter
val - a non-null instance of a persistent class

setResultTransformer

Query setResultTransformer(org.hibernate.transform.ResultTransformer transformer)
Set a strategy for handling the query results. This can be used to change "shape" of the query result.

Parameters:
transformer - The transformer to apply
Returns:
this (for method chaining)


Copyright © 2004-2009 The SmartWeb Team. All Rights Reserved.