I need to convert a Hibernate criteria query like the following
curList = session.createCriteria(Islem.class)
.createAlias("workingDay", "d")
.setProjection(Projections.sum("amount"))
.add(Restrictions.eq("currency", CURRENCY))
.add(Restrictions.eq("product", product))
.add(Restrictions.ne("status", INACTIVE))
.add(Restrictions.eq("d.status", ACTIVE))
.getResultList();
However in JPA (2) I have no idea how to implement the projection - in this case - the sum. It's odd that Hibernate and JPA (even Hibernate JPA 2) have this tremendous differences especially in criteria queries.
I start by
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Islem> cq = cb.createQuery(Islem.class);
Root<Islem> isr = cq.from(Islem.class);
cq.select(isr).where(cb.equal(isr.get("currency"), CURRENCY),
cb.notEqual(isr.get("status"), INACTIVE),
cb.equal(isr.get("product"), product));
however have no idea how to implement the projection here neither the alias
It's an old question, but let's give an example:
With
CriteriaBuilder
, unlike Hibernate, you always start off with the type of the result you want to query and then construct the projection.On the other hand if you wanted to query for the actual "amount" values, you could do: