0

I'm building query at runtime using criteriaBuilder like that.

Filter filters object by a couple of factors: date between start date and finish date, string field matches one of the strings from set etc.

Predicate[] predicates = buildPredicates(filter, criteriaBuilder, root);
criteriaQuery.select(root).where(predicates);

TypedQuery<MonitorStatusHistory> query = entityManager.createQuery(criteriaQuery);

if (filter.getPage() != null && filter.getSize() != null) {
    query
           .setFirstResult(filter.getPage() * filter.getSize())
           .setMaxResults(filter.getSize());
}

return query.getResultList();

How can I achieve the same using spring data jpa tools from JPA Repository?

I need to support queries like

SELECT * FROM obj WHERE obj.date > filter.startDate and 
obj.date < filter.finishDate and obj.num=12 or obj.num=32

Let's assume that filter class has following properties:

private Set<Integers> allowedNums; (allowed values for obj.num)
private ZonedDateTime startDate; (obj.date > this)
private ZonedDateTime finishDate; (obj.date < this)
private Integer page; (pagination page)
private Integer size; (pagination objs per page)
20 fps
  • 342
  • 5
  • 18

1 Answers1

1

You can have a simple solution for that or a complicated, one. Unfortunately it is too long provide a solution in this, I have experience with the latter, that definitely works.

m4gic
  • 1,461
  • 12
  • 19