First of all, hello to everybody, I’m more than happy to ask my first question here.
I have a spring app with the user
entity and the users
table. I would like to get a number of all users grouped by certain fields (not per group but in total ). In sql It would be:
select
count(*) OVER () as totalRecords
from users u
group by
u.first_name,
u.last_name,
u.age
order by u.age DESC
OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY;
But I really can’t do that using hibernate criteria. I could do something like:
public Long getTotalCount() {
ProjectionList groupBy = projectionList();
groupBy.add(groupProperty("firstName"), "first_name");
groupBy.add(groupProperty("last_name"), "last_name");
groupBy.add(groupProperty("age"), "age");
groupBy.add(Projections.rowCount());
return (Long) getSession().createCriteria("User")
.setProjection(groupBy)
.uniqueResult();
}
but it’s not what I want. It does counting per each group, I would like to count rows that are the result of the group by
clause