1

In MySQL I'm able to select MAX value for desired column, group and order records, limit result and get total rows count in two SQL statements (executions):

SELECT SQL_CALC_FOUND_ROWS b.class, b.age FROM (
    SELECT students.*, max(age) FROM `students`GROUP BY class LIMIT 0,10
) as b;
SELECT FOUND_ROWS() as total;

From 250 records it selects 24 as total which is correct. (24 unique classes)

I'm able to select grouped records but not sure how to get total count. Also, result is List< List< Student > + MAX(age) >. However, without groupBy and max(), it produces List< Student > as expected.

CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery q = qb.createQuery();

Root e = q.from(Student.class);
Expression maxExpression = qb.max(e.get("age"));

CriteriaQuery<Object> cq = q.multiselect(e, maxExpression);
cb.groupBy(e.get("class"));

TypedQuery typedQuery = em.createQuery(cb);
List<Student> students = typedQuery.getResultList();

Is it possible to get same result in JPA2 as a List?

4

0 回答 0