JPA Criteria select all instances with max values in their groups
Is there a way to write with JPA 2 CriteriaBuilder the equivalent of the following query? select * from season s1 where end = ( select max(end) from season s2 where s1.contest_id=s2.contest_id ); In JPQL this query is: Select s1 from Season s1 where s1.end = ( select max(s2.end) from Season s2 where s1.contest=s2.contest ) This should work, with contest being either a basic Integer property, or a ManyToOne property pointing to another non-basic Entity. EntityManger em; //to be injected or constructed CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Season> cq = cb.createQuery(Season