views:

584

answers:

1

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.

+2  A: 

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();
Buchi
Arghhh. I new it was a simple mistake.
Tim