org.hibernate.impl

Class AbstractQueryImpl

Implemented Interfaces:
Query
Known Direct Subclasses:
QueryImpl, SQLQueryImpl

public abstract class AbstractQueryImpl
extends java.lang.Object
implements Query

Abstract implementation of the Query interface.
Authors:
Gavin King
Max Andersen

Field Summary

protected ParameterMetadata
parameterMetadata
protected SessionImplementor
session

Constructor Summary

AbstractQueryImpl(String queryString, FlushMode flushMode, SessionImplementor session, ParameterMetadata parameterMetadata)

Method Summary

protected void
after()
protected void
before()
protected Type
determineType(String paramName, Class clazz)
protected Type
determineType(String paramName, Object paramValue)
protected Type
determineType(String paramName, Object paramValue, Type defaultType)
protected Type
determineType(int paramPosition, Object paramValue)
protected Type
determineType(int paramPosition, Object paramValue, Type defaultType)
protected String
expandParameterLists(Map namedParamsCopy)
Warning: adds new parameters to the argument by side-effect, as well as mutating the query string!
protected abstract Map
getLockModes()
protected Map
getNamedParameterLists()
Retreive the value map for any named parameter lists (i.e., for auto-expansion) bound to this query.
String[]
getNamedParameters()
Returns an array representing all named parameter names encountered during (intial) parsing of the query.
protected Map
getNamedParams()
Returns a shallow copy of the named parameter value map.
QueryParameters
getQueryParameters(Map namedParams)
String
getQueryString()
Get the query string.
String[]
getReturnAliases()
Return the HQL select clause aliases (if any)
Type[]
getReturnTypes()
Return the Hibernate types of the query result set.
protected RowSelection
getRowSelection()
RowSelection
getSelection()
protected List
getTypes()
Retreives the list of parameter types bound to this query for ordinal parameters.
protected List
getValues()
Retreives the list of parameter values bound to this query for ordinal parameters.
boolean
hasNamedParameters()
Does this query contain named parameters?
boolean
isReadOnly()
Query
setBigDecimal(String name, BigDecimal number)
Query
setBigDecimal(int position, BigDecimal number)
Query
setBigInteger(String name, BigInteger number)
Query
setBigInteger(int position, BigInteger number)
Query
setBinary(String name, byte[] val)
Query
setBinary(int position, byte[] val)
Query
setBoolean(String name, boolean val)
Query
setBoolean(int position, boolean val)
Query
setByte(String name, byte val)
Query
setByte(int position, byte val)
Query
setCacheMode(CacheMode cacheMode)
Override the current session cache mode, just for this query.
Query
setCacheRegion(String cacheRegion)
Set the name of the cache region.
Query
setCacheable(boolean cacheable)
Enable caching of this query result set.
Query
setCalendar(String name, Calendar calendar)
Query
setCalendar(int position, Calendar calendar)
Query
setCalendarDate(String name, Calendar calendar)
Query
setCalendarDate(int position, Calendar calendar)
Query
setCharacter(String name, char val)
Query
setCharacter(int position, char val)
Query
setCollectionKey(Serializable collectionKey)
Query
setComment(String comment)
Add a comment to the generated SQL.
Query
setDate(String name, Date date)
Query
setDate(int position, Date date)
Query
setDouble(String name, double val)
Query
setDouble(int position, double val)
Query
setEntity(String name, Object val)
Bind an instance of a mapped persistent class to a named query parameter.
Query
setEntity(int position, Object val)
Bind an instance of a mapped persistent class to a JDBC-style 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(String name, float val)
Query
setFloat(int position, float val)
Query
setFlushMode(FlushMode flushMode)
Override the current session flush mode, just for this query.
Query
setInteger(String name, int val)
Query
setInteger(int position, int val)
Query
setLocale(String name, Locale locale)
Query
setLocale(int position, Locale locale)
Query
setLong(String name, long val)
Query
setLong(int position, long val)
Query
setMaxResults(int maxResults)
Set the maximum number of rows to retrieve.
void
setOptionalEntityName(String optionalEntityName)
void
setOptionalId(Serializable optionalId)
void
setOptionalObject(Object optionalObject)
Query
setParameter(String name, Object val)
Bind a value to a named query parameter.
Query
setParameter(String name, Object val, Type type)
Bind a value to a named query parameter.
Query
setParameter(int position, Object val)
Bind a value to a JDBC-style query parameter.
Query
setParameter(int position, Object val, Type type)
Bind a value to a JDBC-style query parameter.
Query
setParameterList(String name, Collection vals)
Bind multiple values to a named query parameter.
Query
setParameterList(String name, Collection vals, Type type)
Bind multiple values to a named query parameter.
Query
setParameterList(String name, Object[] vals)
Bind multiple values to a named query parameter.
Query
setParameterList(String name, Object[] vals, Type type)
Bind multiple values to a named query parameter.
Query
setParameters(Object[] values, Type[] types)
Bind values and types to positional parameters.
Query
setProperties(Map map)
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(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(ResultTransformer transformer)
Set a strategy for handling the query results.
Query
setSerializable(String name, Serializable val)
Query
setSerializable(int position, Serializable val)
Query
setShort(String name, short val)
Query
setShort(int position, short val)
Query
setString(String name, String val)
Query
setString(int position, String val)
Query
setText(String name, String val)
Query
setText(int position, String val)
Query
setTime(String name, Date date)
Query
setTime(int position, Date date)
Query
setTimeout(int timeout)
Set a timeout for the underlying JDBC query.
Query
setTimestamp(String name, Date date)
Query
setTimestamp(int position, Date date)
String
toString()
Type[]
typeArray()
Object
uniqueResult()
Convenience method to return a single instance that matches the query, or null if the query returns no results.
Object[]
valueArray()
protected void
verifyParameters()
Perform parameter validation.
protected void
verifyParameters(boolean reserveFirstParameter)
Perform parameter validation.

Field Details

parameterMetadata

protected final ParameterMetadata parameterMetadata

session

protected final SessionImplementor session

Constructor Details

AbstractQueryImpl

public AbstractQueryImpl(String queryString,
                         FlushMode flushMode,
                         SessionImplementor session,
                         ParameterMetadata parameterMetadata)

Method Details

after

protected void after()

before

protected void before()

determineType

protected Type determineType(String paramName,
                             Class clazz)
            throws HibernateException

determineType

protected Type determineType(String paramName,
                             Object paramValue)
            throws HibernateException

determineType

protected Type determineType(String paramName,
                             Object paramValue,
                             Type defaultType)

determineType

protected Type determineType(int paramPosition,
                             Object paramValue)
            throws HibernateException

determineType

protected Type determineType(int paramPosition,
                             Object paramValue,
                             Type defaultType)

expandParameterLists

protected String expandParameterLists(Map namedParamsCopy)
Warning: adds new parameters to the argument by side-effect, as well as mutating the query string!

getLockModes

protected abstract Map getLockModes()

getNamedParameterLists

protected Map getNamedParameterLists()
Retreive the value map for any named parameter lists (i.e., for auto-expansion) bound to this query.
Returns:
The parameter list value map.

getNamedParameters

public String[] getNamedParameters()
            throws HibernateException
Returns an array representing all named parameter names encountered during (intial) parsing of the query.

Note initial here means different things depending on whether this is a native-sql query or an HQL/filter query. For native-sql, a precursory inspection of the query string is performed specifically to locate defined parameters. For HQL/filter queries, this is the information returned from the query-translator. This distinction holds true for all parameter metadata exposed here.

Specified by:
getNamedParameters in interface Query
Returns:
Array of named parameter names.

getNamedParams

protected Map getNamedParams()
Returns a shallow copy of the named parameter value map.
Returns:
Shallow copy of the named parameter value map

getQueryParameters

public QueryParameters getQueryParameters(Map namedParams)

getQueryString

public final String getQueryString()
Get the query string.
Specified by:
getQueryString in interface Query
Returns:
the query string

getReturnAliases

public String[] getReturnAliases()
            throws HibernateException
Return the HQL select clause aliases (if any)
Specified by:
getReturnAliases in interface Query
Returns:
an array of aliases as strings

getReturnTypes

public Type[] getReturnTypes()
            throws HibernateException
Return the Hibernate types of the query result set.
Specified by:
getReturnTypes in interface Query
Returns:
an array of types

getRowSelection

protected RowSelection getRowSelection()

getSelection

public RowSelection getSelection()

getTypes

protected List getTypes()
Retreives the list of parameter types bound to this query for ordinal parameters.
Returns:
The ordinal parameter types.

getValues

protected List getValues()
Retreives the list of parameter values bound to this query for ordinal parameters.
Returns:
The ordinal parameter values.

hasNamedParameters

public boolean hasNamedParameters()
Does this query contain named parameters?
Returns:
True if the query was found to contain named parameters; false otherwise;

isReadOnly

public boolean isReadOnly()

setBigDecimal

public Query setBigDecimal(String name,
                           BigDecimal number)
Specified by:
setBigDecimal in interface Query

setBigDecimal

public Query setBigDecimal(int position,
                           BigDecimal number)
Specified by:
setBigDecimal in interface Query

setBigInteger

public Query setBigInteger(String name,
                           BigInteger number)
Specified by:
setBigInteger in interface Query

setBigInteger

public Query setBigInteger(int position,
                           BigInteger number)
Specified by:
setBigInteger in interface Query

setBinary

public Query setBinary(String name,
                       byte[] val)
Specified by:
setBinary in interface Query

setBinary

public Query setBinary(int position,
                       byte[] val)
Specified by:
setBinary in interface Query

setBoolean

public Query setBoolean(String name,
                        boolean val)
Specified by:
setBoolean in interface Query

setBoolean

public Query setBoolean(int position,
                        boolean val)
Specified by:
setBoolean in interface Query

setByte

public Query setByte(String name,
                     byte val)
Specified by:
setByte in interface Query

setByte

public Query setByte(int position,
                     byte val)
Specified by:
setByte in interface Query

setCacheMode

public Query setCacheMode(CacheMode cacheMode)
Override the current session cache mode, just for this query.
Specified by:
setCacheMode in interface Query
See Also:
CacheMode

setCacheRegion

public Query setCacheRegion(String cacheRegion)
Set the name of the cache region.
Specified by:
setCacheRegion in interface Query
Parameters:
cacheRegion - the name of a query cache region, or null for the default query cache

setCacheable

public Query setCacheable(boolean cacheable)
Enable caching of this query result set.
Specified by:
setCacheable in interface Query
Parameters:
cacheable - Should the query results be cacheable?

setCalendar

public Query setCalendar(String name,
                         Calendar calendar)
Specified by:
setCalendar in interface Query

setCalendar

public Query setCalendar(int position,
                         Calendar calendar)
Specified by:
setCalendar in interface Query

setCalendarDate

public Query setCalendarDate(String name,
                             Calendar calendar)
Specified by:
setCalendarDate in interface Query

setCalendarDate

public Query setCalendarDate(int position,
                             Calendar calendar)
Specified by:
setCalendarDate in interface Query

setCharacter

public Query setCharacter(String name,
                          char val)
Specified by:
setCharacter in interface Query

setCharacter

public Query setCharacter(int position,
                          char val)
Specified by:
setCharacter in interface Query

setCollectionKey

public Query setCollectionKey(Serializable collectionKey)

setComment

public Query setComment(String comment)
Add a comment to the generated SQL.
Specified by:
setComment in interface Query
Parameters:
comment - a human-readable string

setDate

public Query setDate(String name,
                     Date date)
Specified by:
setDate in interface Query

setDate

public Query setDate(int position,
                     Date date)
Specified by:
setDate in interface Query

setDouble

public Query setDouble(String name,
                       double val)
Specified by:
setDouble in interface Query

setDouble

public Query setDouble(int position,
                       double val)
Specified by:
setDouble in interface Query

setEntity

public Query setEntity(String name,
                       Object val)
Bind an instance of a mapped persistent class to a named query parameter.
Specified by:
setEntity in interface Query
Parameters:
name - the name of the parameter
val - a non-null instance of a persistent class

setEntity

public Query setEntity(int position,
                       Object val)
Bind an instance of a mapped persistent class to a JDBC-style query parameter.
Specified by:
setEntity in interface Query
Parameters:
position - the position of the parameter in the query string, numbered from 0.
val - a non-null instance of a persistent class

setFetchSize

public Query setFetchSize(int fetchSize)
Set a fetch size for the underlying JDBC query.
Specified by:
setFetchSize in interface Query
Parameters:
fetchSize - the fetch size

setFirstResult

public Query setFirstResult(int firstResult)
Set the first row to retrieve. If not set, rows will be retrieved beginnning from row 0.
Specified by:
setFirstResult in interface Query
Parameters:
firstResult - a row number, numbered from 0

setFloat

public Query setFloat(String name,
                      float val)
Specified by:
setFloat in interface Query

setFloat

public Query setFloat(int position,
                      float val)
Specified by:
setFloat in interface Query

setFlushMode

public Query setFlushMode(FlushMode flushMode)
Override the current session flush mode, just for this query.
Specified by:
setFlushMode in interface Query
See Also:
FlushMode

setInteger

public Query setInteger(String name,
                        int val)
Specified by:
setInteger in interface Query

setInteger

public Query setInteger(int position,
                        int val)
Specified by:
setInteger in interface Query

setLocale

public Query setLocale(String name,
                       Locale locale)
Specified by:
setLocale in interface Query

setLocale

public Query setLocale(int position,
                       Locale locale)
Specified by:
setLocale in interface Query

setLong

public Query setLong(String name,
                     long val)
Specified by:
setLong in interface Query

setLong

public Query setLong(int position,
                     long val)
Specified by:
setLong in interface Query

setMaxResults

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.
Specified by:
setMaxResults in interface Query
Parameters:
maxResults - the maximum number of rows

setOptionalEntityName

public void setOptionalEntityName(String optionalEntityName)

setOptionalId

public void setOptionalId(Serializable optionalId)

setOptionalObject

public void setOptionalObject(Object optionalObject)

setParameter

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.
Specified by:
setParameter in interface Query
Parameters:
name - the name of the parameter
val - the non-null parameter value
Throws:
HibernateException - if no type could be determined

setParameter

public Query setParameter(String name,
                          Object val,
                          Type type)
Bind a value to a named query parameter.
Specified by:
setParameter in interface Query
Parameters:
name - the name of the parameter
val - the possibly-null parameter value
type - the Hibernate type

setParameter

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.
Specified by:
setParameter in interface Query
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

public Query setParameter(int position,
                          Object val,
                          Type type)
Bind a value to a JDBC-style query parameter.
Specified by:
setParameter in interface Query
Parameters:
position - the position of the parameter in the query string, numbered from 0.
val - the possibly-null parameter value
type - the Hibernate type

setParameterList

public Query setParameterList(String name,
                              Collection 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).
Specified by:
setParameterList in interface Query
Parameters:
name - the name of the parameter
vals - a collection of values to list

setParameterList

public Query setParameterList(String name,
                              Collection 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).
Specified by:
setParameterList in interface Query
Parameters:
name - the name of the parameter
vals - a collection of values to list
type - the Hibernate type of the values

setParameterList

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).
Specified by:
setParameterList in interface Query
Parameters:
name - the name of the parameter
vals - a collection of values to list

setParameterList

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).
Specified by:
setParameterList in interface Query
Parameters:
name - the name of the parameter
vals - a collection of values to list
type - the Hibernate type of the values

setParameters

public Query setParameters(Object[] values,
                           Type[] types)
Bind values and types to positional parameters.
Specified by:
setParameters in interface Query

setProperties

public Query setProperties(Map map)
            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.
Specified by:
setProperties in interface Query
Parameters:

setProperties

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.
Specified by:
setProperties in interface Query
Parameters:
bean - any JavaBean or POJO

setReadOnly

public 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.
Specified by:
setReadOnly in interface Query

setResultTransformer

public Query setResultTransformer(ResultTransformer transformer)
Set a strategy for handling the query results. This can be used to change "shape" of the query result.
Specified by:
setResultTransformer in interface Query
Parameters:
transformer - The transformer to apply
Returns:
this (for method chaining)

setSerializable

public Query setSerializable(String name,
                             Serializable val)
Specified by:
setSerializable in interface Query

setSerializable

public Query setSerializable(int position,
                             Serializable val)
Specified by:
setSerializable in interface Query

setShort

public Query setShort(String name,
                      short val)
Specified by:
setShort in interface Query

setShort

public Query setShort(int position,
                      short val)
Specified by:
setShort in interface Query

setString

public Query setString(String name,
                       String val)
Specified by:
setString in interface Query

setString

public Query setString(int position,
                       String val)
Specified by:
setString in interface Query

setText

public Query setText(String name,
                     String val)
Specified by:
setText in interface Query

setText

public Query setText(int position,
                     String val)
Specified by:
setText in interface Query

setTime

public Query setTime(String name,
                     Date date)
Specified by:
setTime in interface Query

setTime

public Query setTime(int position,
                     Date date)
Specified by:
setTime in interface Query

setTimeout

public Query setTimeout(int timeout)
Set a timeout for the underlying JDBC query.
Specified by:
setTimeout in interface Query
Parameters:
timeout - the timeout in seconds

setTimestamp

public Query setTimestamp(String name,
                          Date date)
Specified by:
setTimestamp in interface Query

setTimestamp

public Query setTimestamp(int position,
                          Date date)
Specified by:
setTimestamp in interface Query

toString

public String toString()

typeArray

public Type[] typeArray()

uniqueResult

public Object uniqueResult()
            throws HibernateException
Convenience method to return a single instance that matches the query, or null if the query returns no results.
Specified by:
uniqueResult in interface Query
Returns:
the single result or null

valueArray

public Object[] valueArray()

verifyParameters

protected void verifyParameters()
            throws QueryException
Perform parameter validation. Used prior to executing the encapsulated query.
Throws:
QueryException -

verifyParameters

protected void verifyParameters(boolean reserveFirstParameter)
            throws HibernateException
Perform parameter validation. Used prior to executing the encapsulated query.
Parameters:
reserveFirstParameter - if true, the first ? will not be verified since its needed for e.g. callable statements returning a out parameter