I am quite new to JPA, and Criteria API. Currently my goal is to get the max() of a count() agregate.
Criteria code:
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery criteriaQ = builder.createQuery();
Root<Student> s = criteriaQ.from(Student.class);
criteriaQ.groupBy(s);
criteriaQ.multiselect(s, builder.count(s.get(Student_.assistedCourse)));
Relationship:
@OneToMany(mappedBy="ta")
Student_.assistedCourse
@ManyToOne
Course_.ta
In short, the goal is to get the student that has assisted in the most course. The output should be an Object[] containing the highest number of course assisted and the student entity instance, who assisted that number of course.
With normal SQL, I can do a subquery with some alias. But Criteria is a bit confusing to me in how to connect multiple queries together. What is the best practice for case like this?
Thanks in advance.