FileDocCategorySizeDatePackage
Query.javaAPI DocHibernate 3.2.514050Tue Oct 17 03:57:22 BST 2006org.hibernate

Query

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():
  • a particular page of the result set may be selected by calling setMaxResults(), setFirstResult()
  • named query parameters may be used
  • the results may be returned as an instance of ScrollableResults

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.
see
org.hibernate.Session#createQuery(java.lang.String)
see
org.hibernate.ScrollableResults
author
Gavin King

Fields Summary
Constructors Summary
Methods Summary
public intexecuteUpdate()
Execute the update or delete statement.

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

return
The number of entities updated or deleted.
throws
HibernateException

public java.lang.String[]getNamedParameters()
Return the names of all named parameters of the query.

return
the parameter names, in no particular order

public java.lang.StringgetQueryString()
Get the query string.

return
the query string

public java.lang.String[]getReturnAliases()
Return the HQL select clause aliases (if any)

return
an array of aliases as strings

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

return
an array of types

public java.util.Iteratoriterate()
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.

return
the result iterator
throws
HibernateException

public java.util.Listlist()
Return the query results as a List. If the query contains multiple results pre row, the results are returned in an instance of Object[].

return
the result list
throws
HibernateException

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

see
ScrollableResults
return
the result iterator
throws
HibernateException

public ScrollableResultsscroll(ScrollMode scrollMode)
Return the query results as ScrollableResults. The scrollability of the returned results depends upon JDBC driver support for scrollable ResultSets.

see
ScrollableResults
see
ScrollMode
return
the result iterator
throws
HibernateException

public org.hibernate.QuerysetBigDecimal(int position, java.math.BigDecimal number)

public org.hibernate.QuerysetBigDecimal(java.lang.String name, java.math.BigDecimal number)

public org.hibernate.QuerysetBigInteger(int position, java.math.BigInteger number)

public org.hibernate.QuerysetBigInteger(java.lang.String name, java.math.BigInteger number)

public org.hibernate.QuerysetBinary(int position, byte[] val)

public org.hibernate.QuerysetBinary(java.lang.String name, byte[] val)

public org.hibernate.QuerysetBoolean(int position, boolean val)

public org.hibernate.QuerysetBoolean(java.lang.String name, boolean val)

public org.hibernate.QuerysetByte(int position, byte val)

public org.hibernate.QuerysetByte(java.lang.String name, byte val)

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

see
org.hibernate.CacheMode

public org.hibernate.QuerysetCacheRegion(java.lang.String cacheRegion)
Set the name of the cache region.

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

public org.hibernate.QuerysetCacheable(boolean cacheable)
Enable caching of this query result set.

param
cacheable Should the query results be cacheable?

public org.hibernate.QuerysetCalendar(int position, java.util.Calendar calendar)

public org.hibernate.QuerysetCalendar(java.lang.String name, java.util.Calendar calendar)

public org.hibernate.QuerysetCalendarDate(int position, java.util.Calendar calendar)

public org.hibernate.QuerysetCalendarDate(java.lang.String name, java.util.Calendar calendar)

public org.hibernate.QuerysetCharacter(int position, char val)

public org.hibernate.QuerysetCharacter(java.lang.String name, char val)

public org.hibernate.QuerysetComment(java.lang.String comment)
Add a comment to the generated SQL.

param
comment a human-readable string

public org.hibernate.QuerysetDate(int position, java.util.Date date)

public org.hibernate.QuerysetDate(java.lang.String name, java.util.Date date)

public org.hibernate.QuerysetDouble(int position, double val)

public org.hibernate.QuerysetDouble(java.lang.String name, double val)

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

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

public org.hibernate.QuerysetEntity(java.lang.String name, java.lang.Object val)
Bind an instance of a mapped persistent class to a named query parameter.

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

public org.hibernate.QuerysetFetchSize(int fetchSize)
Set a fetch size for the underlying JDBC query.

param
fetchSize the fetch size

public org.hibernate.QuerysetFirstResult(int firstResult)
Set the first row to retrieve. If not set, rows will be retrieved beginnning from row 0.

param
firstResult a row number, numbered from 0

public org.hibernate.QuerysetFloat(int position, float val)

public org.hibernate.QuerysetFloat(java.lang.String name, float val)

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

see
org.hibernate.FlushMode

public org.hibernate.QuerysetInteger(int position, int val)

public org.hibernate.QuerysetInteger(java.lang.String name, int val)

public org.hibernate.QuerysetLocale(int position, java.util.Locale locale)

public org.hibernate.QuerysetLocale(java.lang.String name, java.util.Locale locale)

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

param
alias a query alias, or this for a collection filter

public org.hibernate.QuerysetLong(int position, long val)

public org.hibernate.QuerysetLong(java.lang.String name, long val)

public org.hibernate.QuerysetMaxResults(int maxResults)
Set the maximum number of rows to retrieve. If not set, there is no limit to the number of rows retrieved.

param
maxResults the maximum number of rows

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

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

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

param
name the name of the parameter
param
val the possibly-null parameter value
param
type the Hibernate type

public org.hibernate.QuerysetParameter(int position, java.lang.Object val)
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.

param
position the position of the parameter in the query string, numbered from 0.
param
val the non-null parameter value
throws
org.hibernate.HibernateException if no type could be determined

public org.hibernate.QuerysetParameter(java.lang.String name, java.lang.Object val)
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.

param
name the name of the parameter
param
val the non-null parameter value
throws
org.hibernate.HibernateException if no type could be determined

public org.hibernate.QuerysetParameterList(java.lang.String name, java.util.Collection vals, org.hibernate.type.Type type)
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).

param
name the name of the parameter
param
vals a collection of values to list
param
type the Hibernate type of the values

public org.hibernate.QuerysetParameterList(java.lang.String name, java.util.Collection vals)
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).

param
name the name of the parameter
param
vals a collection of values to list

public org.hibernate.QuerysetParameterList(java.lang.String name, java.lang.Object[] vals, org.hibernate.type.Type type)
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).

param
name the name of the parameter
param
vals a collection of values to list
param
type the Hibernate type of the values

public org.hibernate.QuerysetParameterList(java.lang.String name, java.lang.Object[] vals)
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).

param
name the name of the parameter
param
vals a collection of values to list

public org.hibernate.QuerysetParameters(java.lang.Object[] values, org.hibernate.type.Type[] types)
Bind values and types to positional parameters.

public org.hibernate.QuerysetProperties(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.

param
bean any JavaBean or POJO

public org.hibernate.QuerysetProperties(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.

param
bean a java.util.Map

public org.hibernate.QuerysetReadOnly(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.

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

param
transformer The transformer to apply
return
this (for method chaining)

public org.hibernate.QuerysetSerializable(int position, java.io.Serializable val)

public org.hibernate.QuerysetSerializable(java.lang.String name, java.io.Serializable val)

public org.hibernate.QuerysetShort(int position, short val)

public org.hibernate.QuerysetShort(java.lang.String name, short val)

public org.hibernate.QuerysetString(int position, java.lang.String val)

public org.hibernate.QuerysetString(java.lang.String name, java.lang.String val)

public org.hibernate.QuerysetText(int position, java.lang.String val)

public org.hibernate.QuerysetText(java.lang.String name, java.lang.String val)

public org.hibernate.QuerysetTime(int position, java.util.Date date)

public org.hibernate.QuerysetTime(java.lang.String name, java.util.Date date)

public org.hibernate.QuerysetTimeout(int timeout)
Set a timeout for the underlying JDBC query.

param
timeout the timeout in seconds

public org.hibernate.QuerysetTimestamp(int position, java.util.Date date)

public org.hibernate.QuerysetTimestamp(java.lang.String name, java.util.Date date)

public java.lang.ObjectuniqueResult()
Convenience method to return a single instance that matches the query, or null if the query returns no results.

return
the single result or null
throws
NonUniqueResultException if there is more than one matching result