How to do a distinct count in JPA critera API?

Piotr picture Piotr · Jun 1, 2011 · Viewed 23.4k times · Source

I would like to do this but with the criteria API instead:

select count(distinct e) from Event e, IN(e.userAccessPermissions) p where p.principal = :principal and p.permission in (:permissions)

Any ideas?

Answer

Jakub Kahovec picture Jakub Kahovec · Nov 3, 2011

You can use countDistinct on CriteriaBuilder

criteriaQuery.select(criteriaBuilder.countDistinct(entityRoot))