Class TypedQueryBuilder<T>

java.lang.Object
org.broadleafcommerce.common.util.dao.TypedQueryBuilder<T>
Type Parameters:
T - the class that is being queried for

public class TypedQueryBuilder<T> extends Object
Utility class to construct typed query-language queries. This has an advantage over CriteriaQuery in that it will be automatically responsive to polymorphism thanks to Hibernate's handling of query-language strings.
Author:
Andre Azzolini (apazzolini)
  • Field Details

  • Constructor Details

    • TypedQueryBuilder

      public TypedQueryBuilder(Class<T> rootClass, String rootAlias)
      Creates a new TypedQueryBuilder that will utilize the rootAlias as the named object of the class
      Parameters:
      rootClass -
      rootAlias -
    • TypedQueryBuilder

      public TypedQueryBuilder(Class rootClass, String rootAlias, Class<T> returnClass)
  • Method Details

    • addRestriction

      public TypedQueryBuilder<T> addRestriction(String expression, String operation, Object parameter)
      Adds a simple restriction to the query. Note that all restrictions present on the TypedQueryBuilder will be joined with an AND clause.
      Parameters:
      expression -
      operation -
      parameter -
    • addRestriction

      public TypedQueryBuilder<T> addRestriction(TQRestriction restriction)
      Adds an explicit TQRestriction object. Note that all restrictions present on the TypedQueryBuilder will be joined with an AND clause.
      Parameters:
      restriction -
      Returns:
    • addJoin

      public TypedQueryBuilder<T> addJoin(TQJoin join)
    • addOrder

      public TypedQueryBuilder<T> addOrder(TQOrder order)
    • toQueryString

      public String toQueryString()
      Generates the query string based on the current contents of this builder. As the string is generated, this method will also populate the paramMap, which binds actual restriction values.

      Note that this method should typically not be invoked through DAOs. Instead, utilize toQuery(EntityManager), which will automatically generate the TypedQuery and populate the required parameters.

      Returns:
      the QL string
    • toQueryString

      public String toQueryString(boolean count)
      Generates the query string based on the current contents of this builder. As the string is generated, this method will also populate the paramMap, which binds actual restriction values.

      Note that this method should typically not be invoked through DAOs. Instead, utilize toQuery(EntityManager), which will automatically generate the TypedQuery and populate the required parameters.

      If you are using this as a COUNT query, you should look at the corresponding toCountQuery(EntityManager)

      Parameters:
      count - - whether or not the resulting query string should be used as a count query or not
      Returns:
      the QL string
    • getSelectClause

      protected StringBuilder getSelectClause(StringBuilder sb, boolean count)
      Adds the select query from toQueryString()
      Returns:
      sb with the select query appended to it
    • toQuery

      public jakarta.persistence.TypedQuery<T> toQuery(jakarta.persistence.EntityManager em)
      Returns a TypedQuery that represents this builder object. It will already have all of the appropriate parameter values set and is able to be immediately queried against.
      Parameters:
      em -
      Returns:
      the TypedQuery
    • toCountQuery

      public jakarta.persistence.TypedQuery<Long> toCountQuery(jakarta.persistence.EntityManager em)
    • fillParameterMap

      protected void fillParameterMap(jakarta.persistence.TypedQuery<?> q)
    • getParamMap

      public Map<String,Object> getParamMap()
      Returns:
      the paramMap