Class AbstractHqlQuery

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

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, 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 Details

    • 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.
    • lastIdDc

      private DbControl lastIdDc
      The most recent DbControl used to build/execute the ID query.
      Since:
      3.5
    • lastMainQuery

      private Query<?> lastMainQuery
      The most recent main query.
    • lastCountQuery

      private Query<Long> lastCountQuery
      The most recent count query.
    • lastIdQuery

      private Query<Integer> lastIdQuery
      The most recent id query.
      Since:
      3.5
    • cacheResults

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

      private static final Set<Integer> ZERO_SET
  • Constructor Details

    • AbstractHqlQuery

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

    • 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:
    • 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:
    • 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

      <R> Query<R> getMainHqlQuery(DbControl dc, Class<R> returnType)
      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.Query object
    • getCountHqlQuery

      Query<Long> 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.Query object
    • getIdHqlQuery

      Query<Integer> getIdHqlQuery(DbControl dc)
      Build the ID 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.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

      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(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