0

I need to make a JPQL query with aggregate functions, using Specifications. I would like something like

myQuery="select sum(et.durationMinutes/et.percent) from EmployeeTimekeeping et"
myQuery.where(specifications)
myQuery.getResultList();

I'm trying to use a Specifications object instead of my where clause. Is there any way to do something like this?

Thanks!

snieguu
  • 2,073
  • 2
  • 20
  • 39

1 Answers1

0

Expression in CriteriaBuilder

To write query like that you could use Expression class. For example to do sum like that you could write :

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Number> q = cb.createQuery(Number.class);
Root<EmployeeTimekeeping > t = q.from(EmployeeTimekeeping .class);

Expression<Double> sum = cb.sum(t.<Double>get("durationMinutes"), t.<Double>get("percent"));
q.select(sum.alias("sum"));

to query built like that you could add your specification however you want to.

Question like yours was asked already, from what I've read, easiest solution to build dynamic where clause is to use CriteriaAPI.

Emil Hotkowski
  • 2,233
  • 1
  • 13
  • 19