2.10.1: 2009-03-24

net.sf.basedb.core
Class AbstractEntityQuery

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

abstract class AbstractEntityQuery
extends AbstractQuery
implements EntityQuery

An abstract implementation of the EntityQuery interface. This class doesn't support adding selections, groups or having restrictions.

Include options and permission control are implemented using Hibernate filters. See QueryRuntimeFilterFactory.

Version:
2.0
Author:
Nicklas
Last modified
$Date: 2008-10-24 13:43:13 +0200 (Fri, 24 Oct 2008) $

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  QueryRuntimeFilterManager filterManager
          The manager of the runtime query filters.
private  EnumSet<Include> includes
          Flags specifying which items to include.
private  DbControl lastCountDc
          The most recent DbControl used to execute the count query.
private  Query lastCountQuery
          The most recent count query.
private  DbControl lastMainDc
          The most recent DbControl used to execute the main query.
private  Query lastMainQuery
          The most recent main query.
private static Logger logParam
          Log all parameter bindings to prepared statement.
private  QueryRuntimeFilter optionalFilter
          Optional runtime filter.
private  Permission permission
          The logged in user's permission on the items.
private  QueryRuntimeFilter requiredFilter
          The required runtime filter.
private  Item returnType
          The item type returned by the query, eg.
private  Item rootType
          The root item type we start with in the query. eq.
private  boolean stateless
          If the query should be executed in a stateless session or not.
private static Set<Integer> ZERO_SET
           
 
Constructor Summary
AbstractEntityQuery(Item returnType, Item rootType, String rootName, String select, boolean stateless, QueryRuntimeFilter optionalFilter)
          Create a new query that may have different return type and root item type.
AbstractEntityQuery(Item returnType, String entityName, boolean stateless)
          Create a new query returning items of the specified item type using the default optional runtime filter.
AbstractEntityQuery(Item returnType, String entityName, boolean stateless, QueryRuntimeFilter optionalFilter)
          Create a new query returning items of the specified type with a non-default optional runtime filter.
 
Method Summary
 long count(DbControl dc)
          Count the number of items/rows that are returned by the query.
(package private)  void disableFilters(DbControl dc)
          Disable all enabled runtime query filters for the query.
(package private)  void enableFilters(DbControl dc)
          Enable runtime query filters for the query.
 void exclude(Collection<Include> includes)
           
 void exclude(Include... excludes)
          Specify options for which items to exclude from the result.
(package private)  Query getCountHqlQuery(DbControl dc)
          Build the count query and set parameter values for it.
 Permission getItemPermission()
          The logged in user's permission on the items returned by this query.
 Item getItemType()
          The type of items the query returns.
(package private)  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.
 Item getRootType()
          The type of items that is the root of the query.
 void group(Expression expression)
          Not supported.
 void groupPermanent(Expression expression)
          Not supported.
 void having(Restriction restriction)
          Not supported.
 void havingPermanent(Restriction restriction)
          Not supported.
 void include(Collection<Include> includes)
           
 void include(Include... includes)
          Specify options for which items to include in the result.
 boolean isCachingResult()
          If the query results are cached or not.
 boolean isIncluded(Collection<Include> includes)
           
 boolean isIncluded(Include... includes)
          Check which options are set for the items to include in the result
 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.
 void reset()
          Reset all non-permanent query elements of the query and clear cached queries.
 void select(Select select)
          Not supported.
 void selectPermanent(Select select)
          Not supported.
 void setAutoJoinType(JoinType joinType)
          Specify the join type of automatic joins.
 void setCacheResult(boolean flag)
          Specify if the query results should be cached or not.
 void setItemPermission(Permission permission)
          Only return items which the logged in user has the specified permission for.
private  void setParameters(Query query, Map<String,QueryParameter> parameters)
          Set parameter values on a query.
 
Methods inherited from class net.sf.basedb.core.AbstractQuery
addAutoJoiner, getCountQuery, getFirstResult, getMainQuery, getMaxResults, getParameterNames, getParameters, getParameterType, getParameterValue, getPermanentSelects, getQueryParameter, getQuerySection, getSelects, hasParameterValue, isCounting, isDistinct, isReturningTotalCount, join, joinPermanent, order, orderPermanent, resetTemporary, restrict, restrictPermanent, 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, hasParameterValue, isCounting, isDistinct, isReturningTotalCount, join, joinPermanent, order, orderPermanent, restrict, restrictPermanent, 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()


returnType

private final Item returnType
The item type returned by the query, eg. SELECT [returnType] FROM ...


rootType

private final Item rootType
The root item type we start with in the query. eq. SELECT ... FROM [rootTypeTable]

Since:
2.8

stateless

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


requiredFilter

private final QueryRuntimeFilter requiredFilter
The required runtime filter.


optionalFilter

private final QueryRuntimeFilter optionalFilter
Optional runtime filter.


filterManager

private QueryRuntimeFilterManager filterManager
The manager of the runtime query filters.


lastMainDc

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


lastCountDc

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


lastMainQuery

private Query lastMainQuery
The most recent main query.


lastCountQuery

private Query lastCountQuery
The most recent count query.


cacheResults

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


includes

private EnumSet<Include> includes
Flags specifying which items to include.


permission

private Permission permission
The logged in user's permission on the items.


ZERO_SET

private static final Set<Integer> ZERO_SET
Constructor Detail

AbstractEntityQuery

AbstractEntityQuery(Item returnType,
                    String entityName,
                    boolean stateless)
Create a new query returning items of the specified item type using the default optional runtime filter.

Parameters:
returnType - The type of items that are returned
entityName - The name of the type of items as known to Hibernate, or null to use the class of the return type
stateless - TRUE if the stateless Hibernate session should be used, FALSE if the regular Hibernate session should be used

AbstractEntityQuery

AbstractEntityQuery(Item returnType,
                    String entityName,
                    boolean stateless,
                    QueryRuntimeFilter optionalFilter)
Create a new query returning items of the specified type with a non-default optional runtime filter.

Parameters:
returnType - The type of items that are returned
entityName - The name of the type of items as known to Hibernate, or null to use the class of the return type
stateless - TRUE if the stateless Hibernate session should be used, FALSE if the regular Hibernate session should be used
optionalFilter - A runtime filter replacing the default optional filter or null to not use any optional filter

AbstractEntityQuery

AbstractEntityQuery(Item returnType,
                    Item rootType,
                    String rootName,
                    String select,
                    boolean stateless,
                    QueryRuntimeFilter optionalFilter)
Create a new query that may have different return type and root item type.

Since:
2.8
See Also:
DataQuery.DataQuery(Class, Item, String, String)
Method Detail

select

public void select(Select select)
Not supported.

Specified by:
select in interface Query
Overrides:
select in class AbstractQuery
Parameters:
select - The query element to add
Throws:
UnsupportedOperationException - Always
See Also:
Query.selectPermanent(Select)

selectPermanent

public void selectPermanent(Select select)
Not supported.

Specified by:
selectPermanent in interface Query
Overrides:
selectPermanent in class AbstractQuery
Parameters:
select - The query element to add
Throws:
UnsupportedOperationException - Always
See Also:
Query.select(Select)

group

public void group(Expression expression)
Not supported.

Specified by:
group in interface Query
Overrides:
group in class AbstractQuery
Parameters:
expression - The query element to add
Throws:
UnsupportedOperationException - Always
See Also:
Query.groupPermanent(Expression)

groupPermanent

public void groupPermanent(Expression expression)
Not supported.

Specified by:
groupPermanent in interface Query
Overrides:
groupPermanent in class AbstractQuery
Parameters:
expression - The query element to add
Throws:
UnsupportedOperationException - Always
See Also:
Query.group(Expression)

having

public void having(Restriction restriction)
Not supported.

Specified by:
having in interface Query
Overrides:
having in class AbstractQuery
Parameters:
restriction - The query element to add
Throws:
UnsupportedOperationException - Always
See Also:
Query.havingPermanent(Restriction)

havingPermanent

public void havingPermanent(Restriction restriction)
Not supported.

Specified by:
havingPermanent in interface Query
Overrides:
havingPermanent in class AbstractQuery
Parameters:
restriction - The query element to add
Throws:
UnsupportedOperationException - Always
See Also:
Query.having(Restriction)

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)

getItemType

public Item getItemType()
Description copied from interface: EntityQuery
The type of items the query returns.

Specified by:
getItemType in interface EntityQuery

getRootType

public Item getRootType()
Description copied from interface: EntityQuery
The type of items that is the root of the query. In most cases, this is the same as the EntityQuery.getItemType(), but not always.

Specified by:
getRootType in interface EntityQuery
Since:
2.8

include

public void include(Include... includes)
Description copied from interface: EntityQuery
Specify options for which items to include in the result.

Specified by:
include in interface EntityQuery
Parameters:
includes - An array of items that should be included
See Also:
EntityQuery.exclude(Include[])

include

public void include(Collection<Include> includes)
Specified by:
include in interface EntityQuery
See Also:
EntityQuery.include(Include[])

exclude

public void exclude(Include... excludes)
Description copied from interface: EntityQuery
Specify options for which items to exclude from the result.

Specified by:
exclude in interface EntityQuery
Parameters:
excludes - An array of options that should be excluded
See Also:
EntityQuery.include(Include[])

exclude

public void exclude(Collection<Include> includes)
Specified by:
exclude in interface EntityQuery
See Also:
EntityQuery.exclude(Include[])

isIncluded

public boolean isIncluded(Include... includes)
Description copied from interface: EntityQuery
Check which options are set for the items to include in the result

Specified by:
isIncluded in interface EntityQuery
Parameters:
includes - The options to check
Returns:
TRUE If all specified options are included, FALSE otherwise

isIncluded

public boolean isIncluded(Collection<Include> includes)
Specified by:
isIncluded in interface EntityQuery
See Also:
EntityQuery.isIncluded(Include[])

setItemPermission

public void setItemPermission(Permission permission)
Description copied from interface: EntityQuery
Only return items which the logged in user has the specified permission for. The default is to return all items which the user has at least read permission for.

Specified by:
setItemPermission in interface EntityQuery
Parameters:
permission - The required permission

getItemPermission

public Permission getItemPermission()
Description copied from interface: EntityQuery
The logged in user's permission on the items returned by this query.

Specified by:
getItemPermission in interface EntityQuery
Returns:
A Permission object

setAutoJoinType

public void setAutoJoinType(JoinType joinType)
Specify the join type of automatic joins. The default join type is JoinType.LEFT. This setting doesn't affect joins that has been made directly to the the query.

Parameters:
joinType - The type of the automatic joins
Since:
2.8

getMainHqlQuery

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

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
Since:
2.4

enableFilters

void enableFilters(DbControl dc)
Enable runtime query filters for the query.

See Also:
disableFilters(DbControl)

disableFilters

void disableFilters(DbControl dc)
Disable all enabled runtime query filters for the query.

See Also:
enableFilters(DbControl)

setParameters

private void setParameters(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.10.1: 2009-03-24