Creating EJB query with Criteria Builder
Just documenting it for future purposes or any one in need of help
import javax.persistence.EntityManager;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
public class SampleCriteriaBuilder {
EntityManager entityManager = null;
CriteriaBuilder criteriaBuilder;
List<Predicate> andPredicates = new ArrayList<Predicate>();
TypedQuery<YourDTOObjectRoot> query;
public SampleCriteriaBuilder(EntityManager entityManager) {
super();
this.entityManager = entityManager;
criteriaBuilder = this.entityManager.getCriteriaBuilder();
criteriaQuery = criteriaBuilder.createQuery(YourDTOObject.class);
yourDTOObjectRoot = criteriaQuery.from(yourDTOObject.class);
childTable1Root = yourDTOObjectRoot.join("childTable1", JoinType.LEFT);
childTable2Root = yourDTOObjectRoot.join("childTable2", JoinType.LEFT);
}
// Like clause
Predicate likePredicate =
criteriaBuilder.like(childTable1Root.get("someAttribute"),
"%" + value + "%");
andPredicates.add(likePredicate);
// In clause
String commaSepValue = "one,two,three,four";
javax.persistence.criteria.CriteriaBuilder.In<String> inClause =
criteriaBuilder.in(yourDTOObjectRoot.<String>get("someAttribute"));
List commaSepValueList = commaSepValue.split(",");
for (String valueTaken : commaSepValueList) {
inClause.value(valueTaken);
}
andPredicates.add(inClause);
// Or Clause
Predicate orClause =
criteriaBuilder.or(criteriaBuilder.equal(childTable1Root.get("someProperty"), "someValue"), criteriaBuilder.like(childTable1Root.get("someProperty"),"otherValue"));
andPredicates.add(orClause);
//Comparision between two dates
Path timeStampPath = yourDTOObjectRoot.<Date>get("timestamp");
ParameterExpression<Date> dateFromExp = criteriaBuilder.parameter(Date.class);
ParameterExpression<Date> dateToExp = criteriaBuilder.parameter(Date.class);
Predicate predicate_date = criteriaBuilder.between(timeStampPath, dateFromExp, dateToExp);
andPredicates.add(predicate_date);
Predicate[] predicates = (Predicate[])andPredicates.toArray(new Predicate[andPredicates.size()]);
criteriaQuery.where(predicates);
query = entityManager.createQuery(criteriaQuery);
query.setParameter(dateFromExp, dateFrom, TemporalType.DATE);
query.setParameter(dateToExp, dateTo, TemporalType.DATE);
}
Just documenting it for future purposes or any one in need of help
import javax.persistence.EntityManager;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
public class SampleCriteriaBuilder {
EntityManager entityManager = null;
CriteriaBuilder criteriaBuilder;
List<Predicate> andPredicates = new ArrayList<Predicate>();
TypedQuery<YourDTOObjectRoot> query;
public SampleCriteriaBuilder(EntityManager entityManager) {
super();
this.entityManager = entityManager;
criteriaBuilder = this.entityManager.getCriteriaBuilder();
criteriaQuery = criteriaBuilder.createQuery(YourDTOObject.class);
yourDTOObjectRoot = criteriaQuery.from(yourDTOObject.class);
childTable1Root = yourDTOObjectRoot.join("childTable1", JoinType.LEFT);
childTable2Root = yourDTOObjectRoot.join("childTable2", JoinType.LEFT);
}
// Like clause
Predicate likePredicate =
criteriaBuilder.like(childTable1Root.get("someAttribute"),
"%" + value + "%");
andPredicates.add(likePredicate);
// In clause
String commaSepValue = "one,two,three,four";
javax.persistence.criteria.CriteriaBuilder.In<String> inClause =
criteriaBuilder.in(yourDTOObjectRoot.<String>get("someAttribute"));
List commaSepValueList = commaSepValue.split(",");
for (String valueTaken : commaSepValueList) {
inClause.value(valueTaken);
}
andPredicates.add(inClause);
// Or Clause
Predicate orClause =
criteriaBuilder.or(criteriaBuilder.equal(childTable1Root.get("someProperty"), "someValue"), criteriaBuilder.like(childTable1Root.get("someProperty"),"otherValue"));
andPredicates.add(orClause);
//Comparision between two dates
Path timeStampPath = yourDTOObjectRoot.<Date>get("timestamp");
ParameterExpression<Date> dateFromExp = criteriaBuilder.parameter(Date.class);
ParameterExpression<Date> dateToExp = criteriaBuilder.parameter(Date.class);
Predicate predicate_date = criteriaBuilder.between(timeStampPath, dateFromExp, dateToExp);
andPredicates.add(predicate_date);
Predicate[] predicates = (Predicate[])andPredicates.toArray(new Predicate[andPredicates.size()]);
criteriaQuery.where(predicates);
query = entityManager.createQuery(criteriaQuery);
query.setParameter(dateFromExp, dateFrom, TemporalType.DATE);
query.setParameter(dateToExp, dateTo, TemporalType.DATE);
}