I know this is an old question, but I got a satisfactory answer.
JPQL:
//the join clause is necessary, because you cannot use p.category in group by clause directly @NamedQuery(name="Category.topN", query="select c, count(p.id) as uses from Posting p join p.category c group by c order by uses desc ")
Java:
List<Object[]> list = getEntityManager().createNamedQuery("Category.topN", Object[].class) .setMaxResults(10) .getResultList();
source share