Thanks for your post. I've also been having a problem with this. I found that you can do
Code:
CriteriaBuilder builder = emf.getCriteriaBuilder();
CriteriaQuery cQuery = builder.createQuery(Integer.class);
Root<X> root = cQuery.from(X.class);
cQuery.select(builder.count(root));
But the resulting HQL looks like
Quote:
select count(X0_.id) as col_0_0_ from X story0_
Depending on your database server and engine, this might be optimized (because the ID is always non-null, counting on the ID is the same as counting *), but in my particular case (MySQL and MyISAM), this doesn't appear to be the case. Fortunately, your work-around seems to do the trick.
I think that this is more a problem of the JPA API then a problem with Hibernate's implementation. They ought to define a method like builder.countAll(), or builder.countStar().