Package org.hibernate
Interface Query
- All Known Subinterfaces:
SQLQuery
- All Known Implementing Classes:
AbstractQueryImpl,CollectionFilterImpl,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.
- 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 Also:
-
Method Summary
Modifier and TypeMethodDescriptionintExecute the update or delete statement.String[]Return the names of all named parameters of the query.Get the query string.String[]Return the HQL select clause aliases (if any)Type[]Return the Hibernate types of the query result set.booleanShould 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.iterate()Return the query results as an Iterator.list()Return the query results as a List.scroll()Return the query results as ScrollableResults.scroll(ScrollMode scrollMode) Return the query results as ScrollableResults.setBigDecimal(int position, BigDecimal number) setBigDecimal(String name, BigDecimal number) setBigInteger(int position, BigInteger number) setBigInteger(String name, BigInteger number) setBinary(int position, byte[] val) setBoolean(int position, boolean val) setBoolean(String name, boolean val) setByte(int position, byte val) setCacheable(boolean cacheable) Enable caching of this query result set.setCacheMode(CacheMode cacheMode) Override the current session cache mode, just for this query.setCacheRegion(String cacheRegion) Set the name of the cache region.setCalendar(int position, Calendar calendar) setCalendar(String name, Calendar calendar) setCalendarDate(int position, Calendar calendar) setCalendarDate(String name, Calendar calendar) setCharacter(int position, char val) setCharacter(String name, char val) setComment(String comment) Add a comment to the generated SQL.setDouble(int position, double val) Bind an instance of a mapped persistent class to a JDBC-style query parameter.Bind an instance of a mapped persistent class to a named query parameter.setFetchSize(int fetchSize) Set a fetch size for the underlying JDBC query.setFirstResult(int firstResult) Set the first row to retrieve.setFloat(int position, float val) setFlushMode(FlushMode flushMode) Override the current session flush mode, just for this query.setInteger(int position, int val) setInteger(String name, int val) setLockMode(String alias, LockMode lockMode) Set the lockmode for the objects idententified by the given alias that appears in the FROM clause.setLockOptions(LockOptions lockOptions) Set the lock options for the objects idententified by the given alias that appears in the FROM clause.setLong(int position, long val) setMaxResults(int maxResults) Set the maximum number of rows to retrieve.setParameter(int position, Object val) Bind a value to a JDBC-style query parameter.setParameter(int position, Object val, Type type) Bind a value to a JDBC-style query parameter.setParameter(String name, Object val) Bind a value to a named query parameter.setParameter(String name, Object val, Type type) Bind a value to a named query parameter.setParameterList(String name, Object[] vals) Bind multiple values to a named query parameter.setParameterList(String name, Object[] vals, Type type) Bind multiple values to a named query parameter.setParameterList(String name, Collection vals) Bind multiple values to a named query parameter.setParameterList(String name, Collection vals, Type type) Bind multiple values to a named query parameter.setParameters(Object[] values, Type[] types) Bind values and types to positional parameters.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 hueristics.setProperties(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.setReadOnly(boolean readOnly) Set the read-only/modifiable mode for entities and proxies loaded by this Query.setResultTransformer(ResultTransformer transformer) Set a strategy for handling the query results.setSerializable(int position, Serializable val) setSerializable(String name, Serializable val) setShort(int position, short val) setTimeout(int timeout) Set a timeout for the underlying JDBC query.setTimestamp(int position, Date date) setTimestamp(String name, Date date) Convenience method to return a single instance that matches the query, or null if the query returns no results.
-
Method Details
-
getQueryString
String getQueryString()Get the query string.- Returns:
- the query string
-
getReturnTypes
Return the Hibernate types of the query result set.- Returns:
- an array of types
- Throws:
HibernateException
-
getReturnAliases
Return the HQL select clause aliases (if any)- Returns:
- an array of aliases as strings
- Throws:
HibernateException
-
getNamedParameters
Return the names of all named parameters of the query.- Returns:
- the parameter names, in no particular order
- Throws:
HibernateException
-
iterate
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
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:
-
scroll
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:
-
list
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
-
uniqueResult
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 resultHibernateException
-
executeUpdate
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
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
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
-
isReadOnly
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:
- 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:
-
setReadOnly
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- 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:
-
setCacheable
Enable caching of this query result set.- Parameters:
cacheable- Should the query results be cacheable?
-
setCacheRegion
Set the name of the cache region.- Parameters:
cacheRegion- the name of a query cache region, or null for the default query cache
-
setTimeout
Set a timeout for the underlying JDBC query.- Parameters:
timeout- the timeout in seconds
-
setFetchSize
Set a fetch size for the underlying JDBC query.- Parameters:
fetchSize- the fetch size
-
setLockOptions
Set the lock options for the objects idententified by the given alias that appears in the FROM clause. -
setLockMode
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
Add a comment to the generated SQL.- Parameters:
comment- a human-readable string
-
setFlushMode
Override the current session flush mode, just for this query.- See Also:
-
setCacheMode
Override the current session cache mode, just for this query.- See Also:
-
setParameter
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 valuetype- the Hibernate type
-
setParameter
Bind a value to a named query parameter.- Parameters:
name- the name of the parameterval- the possibly-null parameter valuetype- the Hibernate type
-
setParameter
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:
HibernateException- if no type could be determined
-
setParameter
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 parameterval- the non-null parameter value- Throws:
HibernateException- if no type could be determined
-
setParameters
Bind values and types to positional parameters.- Throws:
HibernateException
-
setParameterList
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 parametervals- a collection of values to listtype- the Hibernate type of the values- Throws:
HibernateException
-
setParameterList
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 parametervals- a collection of values to list- Throws:
HibernateException
-
setParameterList
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 parametervals- a collection of values to listtype- the Hibernate type of the values- Throws:
HibernateException
-
setParameterList
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 parametervals- a collection of values to list- Throws:
HibernateException
-
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 hueristics.- Parameters:
bean- any JavaBean or POJO- Throws:
HibernateException
-
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 hueristics.- Parameters:
bean- a java.util.Map- Throws:
HibernateException
-
setString
-
setCharacter
-
setBoolean
-
setByte
-
setShort
-
setInteger
-
setLong
-
setFloat
-
setDouble
-
setBinary
-
setText
-
setSerializable
-
setLocale
-
setBigDecimal
-
setBigInteger
-
setDate
-
setTime
-
setTimestamp
-
setCalendar
-
setCalendarDate
-
setString
-
setCharacter
-
setBoolean
-
setByte
-
setShort
-
setInteger
-
setLong
-
setFloat
-
setDouble
-
setBinary
-
setText
-
setSerializable
-
setLocale
-
setBigDecimal
-
setBigInteger
-
setDate
-
setTime
-
setTimestamp
-
setCalendar
-
setCalendarDate
-
setEntity
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
Bind an instance of a mapped persistent class to a named query parameter.- Parameters:
name- the name of the parameterval- a non-null instance of a persistent class
-
setResultTransformer
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)
-