2.17.2: 2011-06-17

net.sf.basedb.core
Class AbstractHqlQuery

java.lang.Object
  extended by net.sf.basedb.core.AbstractQuery
      extended by net.sf.basedb.core.AbstractHqlQuery
All Implemented Interfaces:
HqlQuery, Query
Direct Known Subclasses:
AbstractEntityQuery

abstract class AbstractHqlQuery
extends AbstractQuery
implements HqlQuery

An abstract implementation of the HqlQuery interface. The class can be used to build and manage just about any type of HQL query. It has no function for executing the query or returning the result. Subclasses may use getMainHqlQuery(DbControl) to generate a Hibernate Query object that is ready to be executed.

Version:
2.15
Author:
Nicklas
Last modified
$Date: 2011-02-04 09:25:16 +0100 (Fri, 04 Feb 2011) $

Field Summary
private  boolean cacheResults
          If the results should be cached or not.
private static boolean debugEnabled
          So we don't always have to call logParam.debug()
private  DbControl lastCountDc
          The most recent DbControl used to build/execute the count query.
private  org.hibernate.Query lastCountQuery
          The most recent count query.
private  DbControl lastMainDc
          The most recent DbControl used to build/execute the main query.
private  org.hibernate.Query lastMainQuery
          The most recent main query.
private static Logger logParam
          Log all parameter bindings to prepared statement.
private  String rootAlias
          The alias for the root table.
private  boolean stateless
          If the query should be executed in a stateless session or not.
private static Set<Integer> ZERO_SET
           
 
Constructor Summary
AbstractHqlQuery(String root, String alias, boolean stateless)
          Create a new HQL query.
 
Method Summary
 long count(DbControl dc)
          Count the number of items/rows that are returned by the query.
(package private)  org.hibernate.Query getCountHqlQuery(DbControl dc)
          Build the count query and set parameter values for it.
(package private)  org.hibernate.Query getMainHqlQuery(DbControl dc)
          Build the main query and set parameter values for it.
 QueryType getQueryType()
          Get the type of query.
 String getRootAlias()
          The alias of the item that is the root of this query.
 boolean isCachingResult()
          If the query results are cached or not.
 boolean isReadonly()
          If this query is readonly and cannot be structurally modified.
protected  boolean isStateless()
          If queries are using the stateless Hibernate session or the regular session.
protected  String postProcessQuery(String query)
          Processes the query so that joined paths that are found in other parts of the query are replaced with the join alias.
private  String replaceWithJoinAliases(String query)
          Replaces parts of the query with joined aliases.
 void reset()
          Reset all non-permanent query elements of the query and clear cached queries.
 void setCacheResult(boolean flag)
          Specify if the query results should be cached or not.
 void setEntityParameter(String name, BasicItem value)
          Set the value of an entity parameter.
private  void setParameters(org.hibernate.Query query, Map<String,QueryParameter> parameters)
          Set parameter values on a query.
 void setPermanentEntityParameter(String name, BasicItem value)
          Permanently set the value of an entity parameter.
 
Methods inherited from class net.sf.basedb.core.AbstractQuery
addAutoJoiner, getCountQuery, getFirstResult, getMainQuery, getMaxResults, getParameterNames, getParameters, getParameterType, getParameterValue, getPermanentSelects, getQueryParameter, getQuerySection, getSelects, group, groupPermanent, hasParameterValue, having, havingPermanent, isCounting, isDistinct, isReturningTotalCount, join, joinPermanent, order, orderPermanent, resetTemporary, restrict, restrictPermanent, select, selectPermanent, setCounting, setDistinct, setFirstResult, setMaxResults, setParameter, setPermanentParameter, setReturnTotalCount, temporaryJoin, temporarySelect, toQl, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.sf.basedb.core.query.Query
addAutoJoiner, getFirstResult, getMaxResults, getParameterNames, getQueryParameter, getQuerySection, group, groupPermanent, hasParameterValue, having, havingPermanent, isCounting, isDistinct, isReturningTotalCount, join, joinPermanent, order, orderPermanent, restrict, restrictPermanent, select, selectPermanent, setDistinct, setFirstResult, setMaxResults, setParameter, setPermanentParameter, setReturnTotalCount, toQl
 

Field Detail

logParam

private static final Logger logParam
Log all parameter bindings to prepared statement.


debugEnabled

private static final boolean debugEnabled
So we don't always have to call logParam.debug()


rootAlias

private final String rootAlias
The alias for the root table. This is a requirement in HQL that is not needed in SQL. SELECT ... FROM [rootTable] as [rootAlias]


stateless

private final boolean stateless
If the query should be executed in a stateless session or not.


lastMainDc

private DbControl lastMainDc
The most recent DbControl used to build/execute the main query.


lastCountDc

private DbControl lastCountDc
The most recent DbControl used to build/execute the count query.


lastMainQuery

private org.hibernate.Query lastMainQuery
The most recent main query.


lastCountQuery

private org.hibernate.Query lastCountQuery
The most recent count query.


cacheResults

private boolean cacheResults
If the results should be cached or not.


ZERO_SET

private static final Set<Integer> ZERO_SET
Constructor Detail

AbstractHqlQuery

AbstractHqlQuery(String root,
                 String alias,
                 boolean stateless)
Create a new HQL query. The root table and alias must be given.

Method Detail

getQueryType

public QueryType getQueryType()
Description copied from interface: Query
Get the type of query. Ie. The query language.

Specified by:
getQueryType in interface Query
Returns:
QueryType.HQL

getRootAlias

public String getRootAlias()
The alias of the item that is the root of this query.

Specified by:
getRootAlias in interface Query
See Also:
Item.getAlias()

isReadonly

public boolean isReadonly()
Description copied from interface: Query
If this query is readonly and cannot be structurally modified. Ie. no more query elements can be added. It is still possible to set parameter values. A call to Query.reset() would unlock the query and allow it to be modified again.

Specified by:
isReadonly in interface Query
Overrides:
isReadonly in class AbstractQuery
Returns:
TRUE if the query is readonly, FALSE otherwise

reset

public void reset()
Reset all non-permanent query elements of the query and clear cached queries.

Specified by:
reset in interface Query
Overrides:
reset in class AbstractQuery

count

public long count(DbControl dc)
           throws BaseException
Description copied from interface: Query
Count the number of items/rows that are returned by the query. The method ignores the Query.getFirstResult() and Query.getMaxResults() settings.

Specified by:
count in interface Query
Returns:
The number of rows/items returned by the query
Throws:
BaseException - If there is an error

setCacheResult

public void setCacheResult(boolean flag)
Description copied from interface: HqlQuery
Specify if the query results should be cached or not.

Specified by:
setCacheResult in interface HqlQuery
Parameters:
flag - TRUE if the query results should be cached, FALSE otherwise

isCachingResult

public boolean isCachingResult()
Description copied from interface: HqlQuery
If the query results are cached or not.

Specified by:
isCachingResult in interface HqlQuery
See Also:
HqlQuery.setCacheResult(boolean)

setEntityParameter

public void setEntityParameter(String name,
                               BasicItem value)
Description copied from interface: HqlQuery
Set the value of an entity parameter. If the value of this parameter has already been set permanently, an InvalidDataException is thrown.

Specified by:
setEntityParameter in interface HqlQuery
Parameters:
name - The name of the parameter
value - The value of the parameter

setPermanentEntityParameter

public void setPermanentEntityParameter(String name,
                                        BasicItem value)
Description copied from interface: HqlQuery
Permanently set the value of an entity parameter. If the value of this parameter has already been set permanently, an InvalidDataException is thrown.

Specified by:
setPermanentEntityParameter in interface HqlQuery
Parameters:
name - The name of the parameter
value - The value of the parameter

getMainHqlQuery

org.hibernate.Query getMainHqlQuery(DbControl dc)
Build the main query and set parameter values for it. If the query has been executed with the same DbControl before and hasn't been reset() the cached query is returned. If the parameters values have been changed the new values are used.

Parameters:
dc - The DbControl to use for executing the query
Returns:
A org.hibernate.Query object

getCountHqlQuery

org.hibernate.Query getCountHqlQuery(DbControl dc)
Build the count query and set parameter values for it. If the query has been executed with the same DbControl before and hasn't been reset() the cached query is returned. If the parameters values have been changed the new values are used.

Parameters:
dc - The DbControl to use for executing the query
Returns:
A org.hibernate.Query object

isStateless

protected boolean isStateless()
If queries are using the stateless Hibernate session or the regular session.

Returns:
TRUE if the stateless session is used, FALSE otherwise

postProcessQuery

protected String postProcessQuery(String query)
Processes the query so that joined paths that are found in other parts of the query are replaced with the join alias.

Overrides:
postProcessQuery in class AbstractQuery

replaceWithJoinAliases

private String replaceWithJoinAliases(String query)
Replaces parts of the query with joined aliases. This is needed for Hibernate to generate correct SQL. Eg. select f from Foo f join f.bar b where f.bar.name = :name needs to be changed to: select f from Foo f join f.bar b where b.name = :name

We implement this by looking for the joined path and replacing it with the joined alias.

For left fetch joins we also need to make sure that no more than one step is joined at a time since the implicit join created by Hibernate is not a fetch join.


setParameters

private void setParameters(org.hibernate.Query query,
                           Map<String,QueryParameter> parameters)
Set parameter values on a query.

Parameters:
query - The Hibernate query object
parameters - A map containing parameter names and values

2.17.2: 2011-06-17