JPA Query selecting only specific columns without using Criteria Query?

Yes, like in plain sql you could specify what kind of properties you want to select: SELECT i.firstProperty, i.secondProperty FROM ObjectName i WHERE i.id=10 Executing this query will return a list of Object[], where each array contains the selected properties of one object. Another way is to wrap the selected properties in a custom object … Read more

In JPA 2, using a CriteriaQuery, how to count results

A query of type MyEntity is going to return MyEntity. You want a query for a Long. CriteriaBuilder qb = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = qb.createQuery(Long.class); cq.select(qb.count(cq.from(MyEntity.class))); cq.where(/*your stuff*/); return entityManager.createQuery(cq).getSingleResult(); Obviously you will want to build up your expression with whatever restrictions and groupings etc you skipped in the example.