Class TypedQueryBuilder<T>
java.lang.Object
org.broadleafcommerce.common.util.dao.TypedQueryBuilder<T>
- Type Parameters:
T- the class that is being queried for
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
Fields -
Constructor Summary
ConstructorsConstructorDescriptionTypedQueryBuilder(Class<T> rootClass, String rootAlias) Creates a new TypedQueryBuilder that will utilize the rootAlias as the named object of the classTypedQueryBuilder(Class rootClass, String rootAlias, Class<T> returnClass) -
Method Summary
Modifier and TypeMethodDescriptionaddRestriction(String expression, String operation, Object parameter) Adds a simple restriction to the query.addRestriction(TQRestriction restriction) Adds an explicit TQRestriction object.protected voidfillParameterMap(jakarta.persistence.TypedQuery<?> q) protected StringBuildergetSelectClause(StringBuilder sb, boolean count) Adds the select query fromtoQueryString()jakarta.persistence.TypedQuery<Long>toCountQuery(jakarta.persistence.EntityManager em) jakarta.persistence.TypedQuery<T>toQuery(jakarta.persistence.EntityManager em) Returns a TypedQuery that represents this builder object.Generates the query string based on the current contents of this builder.toQueryString(boolean count) Generates the query string based on the current contents of this builder.
-
Field Details
-
rootClass
-
returnClass
-
rootAlias
-
restrictions
-
joins
-
orders
-
paramMap
-
-
Constructor Details
-
TypedQueryBuilder
Creates a new TypedQueryBuilder that will utilize the rootAlias as the named object of the class- Parameters:
rootClass-rootAlias-
-
TypedQueryBuilder
-
-
Method Details
-
addRestriction
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
Adds an explicit TQRestriction object. Note that all restrictions present on the TypedQueryBuilder will be joined with an AND clause.- Parameters:
restriction-- Returns:
-
addJoin
-
addOrder
-
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
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
Adds the select query fromtoQueryString()- Returns:
- sb with the select query appended to it
-
toQuery
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
-
fillParameterMap
protected void fillParameterMap(jakarta.persistence.TypedQuery<?> q) -
getParamMap
- Returns:
- the paramMap
-