org.broadleafcommerce.common.util.dao
Class TypedQueryBuilder<T>

java.lang.Object
  extended by 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 Summary
protected  Map<String,Object> paramMap
           
protected  List<TQRestriction> restrictions
           
protected  String rootAlias
           
protected  Class<T> rootClass
           
 
Constructor Summary
TypedQueryBuilder(Class<T> rootClass, String rootAlias)
          Creates a new TypedQueryBuilder that will utilize the rootAlias as the named object of the class
 
Method Summary
 TypedQueryBuilder<T> addRestriction(String expression, String operation, Object parameter)
          Adds a simple restriction to the query.
 TypedQueryBuilder<T> addRestriction(TQRestriction restriction)
          Adds an explicit TQRestriction object.
protected  void fillParameterMap(javax.persistence.TypedQuery<?> q)
           
 Map<String,Object> getParamMap()
           
protected  StringBuilder getSelectClause(StringBuilder sb, boolean count)
          Adds the select query from toQueryString()
 javax.persistence.TypedQuery<Long> toCountQuery(javax.persistence.EntityManager em)
           
 javax.persistence.TypedQuery<T> toQuery(javax.persistence.EntityManager em)
          Returns a TypedQuery that represents this builder object.
 String toQueryString()
          Generates the query string based on the current contents of this builder.
 String toQueryString(boolean count)
          Generates the query string based on the current contents of this builder.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rootClass

protected Class<T> rootClass

rootAlias

protected String rootAlias

restrictions

protected List<TQRestriction> restrictions

paramMap

protected Map<String,Object> paramMap
Constructor Detail

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 -
Method Detail

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:

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:
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 javax.persistence.TypedQuery<T> toQuery(javax.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 javax.persistence.TypedQuery<Long> toCountQuery(javax.persistence.EntityManager em)

fillParameterMap

protected void fillParameterMap(javax.persistence.TypedQuery<?> q)

getParamMap

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


Copyright © 2013. All Rights Reserved.