JPA2 Count(*) Criteria API

Example of using JPA2 Criterial Builder and Count(*) Function:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
Root<Company> company = cq.from(Company.class);
Path<NaceCode> naceCodes = null;
naceCodes = company.join(Company_.topNaceCode);
Predicate naceCodeEqual = cb.equal(naceCodes, naceCode);
cq.where(naceCodeEqual);
Expression<Long> count = cb.count(company);
cq.select(count);
Long size = entityManager.createQuery(cq).getSingleResult();
return size.intValue();

More Examples: Google Books

Eclipse RCP / OSGI Hell with Spring & OpenJPA

everyone(?) has problems with classloader/security issues when working with osgi…

org.apache.openjpa.persistence.PersistenceException: Error extracting class information from “bundleresource://3:1
Caused by: java.io.FileNotFoundException:……

if you want to load a custom persistence.xml (with another name) via springs DefaultPersistenceUnitManager you will probably get a problem with osgi which tries to find you persistence.xml, but can’t (see exception above)

i finally solved the problem writing […]

JPA – Open EntityManager In JSF View

If you are using JPA in your JSF web application and want the same benefits as Spring’s OpenSessionInViewFilter (e.g. avoid lazy loading problems with Hibernate,…) you can use THIS contribution.