Package net.sf.basedb.core
Class AbstractHqlQuery
java.lang.Object
net.sf.basedb.core.AbstractQuery
net.sf.basedb.core.AbstractHqlQuery
- Direct Known Subclasses:
AbstractEntityQuery
,SpecialQuery
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, Class)
to generate a Hibernate Query object that is ready to be executed.- Version:
- 2.15
- Author:
- Nicklas
- Last modified
- $Date: 2018-03-14 14:17:56 +0100 (on, 14 mar 2018) $
-
Field Summary
Modifier and TypeFieldDescriptionprivate boolean
If the results should be cached or not.private static final boolean
So we don't always have to call logParam.debug()private DbControl
The most recent DbControl used to build/execute the count query.The most recent count query.private DbControl
The most recent DbControl used to build/execute the ID query.The most recent id query.private DbControl
The most recent DbControl used to build/execute the main query.private Query<?>
The most recent main query.private static final Logger
Log all parameter bindings to prepared statement.private final String
The alias for the root table.private final boolean
If the query should be executed in a stateless session or not. -
Constructor Summary
ConstructorDescriptionAbstractHqlQuery
(String root, String alias, boolean stateless) Create a new HQL query. -
Method Summary
Modifier and TypeMethodDescriptionlong
Count the number of items/rows that are returned by the query.Build the count query and set parameter values for it.Build the ID query and set parameter values for it.(package private) <R> Query<R>
getMainHqlQuery
(DbControl dc, Class<R> returnType) Build the main query and set parameter values for it.Get the type of query.The alias of the item that is the root of this query.boolean
If the query results are cached or not.boolean
If this query is readonly and cannot be structurally modified.protected boolean
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
(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, getIdQuery, getMainQuery, getMaxResults, getParameterNames, getParameters, getParameterType, getParameterValue, getPermanentSelects, getQueryParameter, getQuerySection, getSelects, group, groupPermanent, hasParameterValue, having, havingPermanent, isCounting, isDistinct, isFailSafe, isReturningTotalCount, join, joinPermanent, order, orderPermanent, resetTemporary, restrict, restrictPermanent, select, selectPermanent, setCounting, setDistinct, setFailSafe, 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, isFailSafe, isReturningTotalCount, join, joinPermanent, order, orderPermanent, restrict, restrictPermanent, select, selectPermanent, setDistinct, setFailSafe, setFirstResult, setMaxResults, setParameter, setPermanentParameter, setReturnTotalCount, toQl
-
Field Details
-
logParam
Log all parameter bindings to prepared statement. -
debugEnabled
private static final boolean debugEnabledSo we don't always have to call logParam.debug() -
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 statelessIf the query should be executed in a stateless session or not. -
lastMainDc
The most recent DbControl used to build/execute the main query. -
lastCountDc
The most recent DbControl used to build/execute the count query. -
lastIdDc
The most recent DbControl used to build/execute the ID query.- Since:
- 3.5
-
lastMainQuery
The most recent main query. -
lastCountQuery
The most recent count query. -
lastIdQuery
The most recent id query.- Since:
- 3.5
-
cacheResults
private boolean cacheResultsIf the results should be cached or not. -
ZERO_SET
-
-
Constructor Details
-
AbstractHqlQuery
Create a new HQL query. The root table and alias must be given.
-
-
Method Details
-
getQueryType
Description copied from interface:Query
Get the type of query. Ie. The query language.- Specified by:
getQueryType
in interfaceQuery
- Returns:
QueryType.HQL
-
getRootAlias
The alias of the item that is the root of this query.- Specified by:
getRootAlias
in interfaceQuery
- See Also:
-
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 toQuery.reset()
would unlock the query and allow it to be modified again.- Specified by:
isReadonly
in interfaceQuery
- Overrides:
isReadonly
in classAbstractQuery
- 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 interfaceQuery
- Overrides:
reset
in classAbstractQuery
-
count
Description copied from interface:Query
Count the number of items/rows that are returned by the query. The method ignores theQuery.getFirstResult()
andQuery.getMaxResults()
settings.- Specified by:
count
in interfaceQuery
- 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 interfaceHqlQuery
- 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 interfaceHqlQuery
- See Also:
-
setEntityParameter
Description copied from interface:HqlQuery
Set the value of an entity parameter. If the value of this parameter has already been set permanently, anInvalidDataException
is thrown.- Specified by:
setEntityParameter
in interfaceHqlQuery
- Parameters:
name
- The name of the parametervalue
- The value of the parameter
-
setPermanentEntityParameter
Description copied from interface:HqlQuery
Permanently set the value of an entity parameter. If the value of this parameter has already been set permanently, anInvalidDataException
is thrown.- Specified by:
setPermanentEntityParameter
in interfaceHqlQuery
- Parameters:
name
- The name of the parametervalue
- The value of the parameter
-
getMainHqlQuery
Build the main query and set parameter values for it. If the query has been executed with the sameDbControl
before and hasn't beenreset()
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.Query
object
-
getCountHqlQuery
Build the count query and set parameter values for it. If the query has been executed with the sameDbControl
before and hasn't beenreset()
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.Query
object
-
getIdHqlQuery
Build the ID query and set parameter values for it. If the query has been executed with the sameDbControl
before and hasn't beenreset()
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.Query
object - Since:
- 3.5
-
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
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 classAbstractQuery
-
replaceWithJoinAliases
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 = :nameWe 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
Set parameter values on a query.- Parameters:
query
- The Hibernate query objectparameters
- A map containing parameter names and values
-