This is my solution:
The DAO class
public List<Outlay> findByParameters(String author, String page, String pageSize,
String type, String minAmount, String maxAmount, String fromDate, String toDate)
throws PersistenceException, ParseException {
EntityManager em = EMF.get().createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Outlay> cq = cb.createQuery(Outlay.class);
Root<Outlay> outlay = cq.from(Outlay.class);
List<Predicate> predicates = new ArrayList<Predicate>();
if(author != null && !author.isEmpty()) {
predicates.add(cb.equal(outlay.get(Outlay_.author),
cb.parameter(String.class, "author")));
}
if(type != null && !type.isEmpty()) {
predicates.add(cb.equal(outlay.get(Outlay_.type),
cb.parameter(String.class, "type")));
}
if(minAmount != null && !minAmount.isEmpty()) {
predicates.add(cb.ge(outlay.get(Outlay_.amount),
cb.parameter(Double.class, "minAmount")));
}
if(maxAmount != null && !maxAmount.isEmpty()) {
predicates.add(cb.le(outlay.get(Outlay_.amount),
cb.parameter(Double.class, "maxAmount")));
}
if(fromDate != null && !fromDate.isEmpty()) {
predicates.add(cb.greaterThanOrEqualTo(outlay.get(Outlay_.date),
cb.parameter(Date.class, "fromDate")));
}
if(toDate != null && !toDate.isEmpty()) {
predicates.add(cb.lessThanOrEqualTo(outlay.get(Outlay_.date),
cb.parameter(Date.class, "toDate")));
}
cq.select(outlay).where(predicates.toArray(new Predicate[]{}));
TypedQuery<Outlay> query = em.createQuery(cq);
if(author != null && !author.isEmpty()) {
query.setParameter("author", author);
}
if(type != null && !type.isEmpty()) {
query.setParameter("type", type);
}
if(minAmount != null && !minAmount.isEmpty()) {
query.setParameter("minAmount", Double.parseDouble(minAmount));
}
if(maxAmount != null && !maxAmount.isEmpty()) {
query.setParameter("maxAmount", Double.parseDouble(maxAmount));
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
if(fromDate != null && !fromDate.isEmpty()) {
query.setParameter("fromDate", formatter.parse(fromDate));
}
if(toDate != null && !toDate.isEmpty()) {
query.setParameter("toDate", formatter.parse(toDate));
}
Integer index = 0;
if(page != null && !page.isEmpty()) {
index = Integer.parseInt(page);
}
Integer step = 10;
if(pageSize != null && !pageSize.isEmpty()) {
step = Integer.parseInt(pageSize);
}
query.setFirstResult(index * step);
query.setMaxResults(step);
List<Outlay> outlayList = new ArrayList<Outlay>();
try {
outlayList = query.getResultList();
log.info("Retrieved " + outlayList.size() + " Outlay");
} finally {
em.close();
}
return outlayList;
}
Then metamodel class added:
package com.mycompany.model;
import java.util.Date;
import javax.persistence.metamodel.SingularAttribute;
@javax.persistence.metamodel.StaticMetamodel(com.mycompany.model.Outlay.class)
public class Outlay_ {
public static volatile SingularAttribute<Outlay, String> author;
public static volatile SingularAttribute<Outlay, String> type;
public static volatile SingularAttribute<Outlay, Double> amount;
public static volatile SingularAttribute<Outlay, Date> date;
}