I'm having trouble figuring out how to represent the following JPQL query:
SELECT count(e) FROM Foo e
using Criteria API. What I'm trying is:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Foo> c = cb.createQuery(Foo.class);
Root<Foo> f = c.from(Foo.class);
c.select(cb.count(f));
but this is not working. I also tried:
c.select(cb.count(f.get("id"));
This is for JPA2, Eclipselink.
try this, this is working with hibernate 3.5.1:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Long> c = cb.createQuery(Long.class);
Root<Foo> f = c.from(Foo.class);
c.select(cb.count(f));
int count = em.createQuery(c).getSingleResult().intValue();
This is a pretty old question but for completness here's a simple addition:
The title said something about "using countDistinct", so countDistinct should be mentioned here:
CriteriaBuilder critBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> critQuery = criteriaBuilder.createQuery(Long.class);
Root<Foo> root = critQuery.from(Foo.class);
critQuery.select(critBuilder.countDistinct(root));
int count = entityManager.createQuery(critQuery).getSingleResult().intValue();
This is important if you don't want to count rows that are double. If you want to avoid doule rows in your ResultList, you'd had to use:
CriteriaBuilder critBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> critQuery = criteriaBuilder.createQuery(Long.class);
Root<Foo> root = critQuery.from(Foo.class);
critQuery.select(root).distinct(true);
List<Foo> result = entityManager.createQuery(critQuery).getResultList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With