Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Using count() with Criteria API

I'm having some trouble getting count() working with Criteria API.
What I have right now using JPQL is:

 SELECT count(e) FROM Foo e

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));

and also:

 CriteriaBuilder cb = em.getCriteriaBuilder();
 CriteriaQuery<Foo> c = cb.createQuery(Foo.class);
 Root<Foo> f = c.from(Foo.class);
 c.select(cb.count(f.get("id")));

Ideally, I'd like to get it working with Metamodel, but I've run into
even more problems with that:

 CriteriaBuilder cb = em.getCriteriaBuilder();
 CriteriaQuery<Foo> c = cb.createQuery(Foo.class);
 Root<Foo> f = c.from(Foo.class);
 Metamodel m = em.getMetamodel();
 EntityType<Foo> Foo_ = m.entity(Foo.class);
 c.select(cb.count(f.get(Foo_.id)));

as "id" is not resolved. I do get at least the get part to work with:

 c.select(cb.count(q.get(Foo_.getSingularAttribute("id",Integer.class))));

I still get a failure on select() as it does not resolve to Selection.
I'd still like to use the previous entry as I don't like having use a
string for the column name.

What am I doing wrong with count()? How can I emulate the JPQL query?
Why isn't the Metamodel EntityType not including the attributes? How
can I get them there so I can avoid using string names to get them?

I'm using Eclipselink 2.0.1.

--Tim


Back to the top