The Criteria API is an API for building queries with Java objects, as an alternative to building strings for Java Persistence Query Language (JPQL) queries.
The Criteria API supports building queries dynamically at run time, and also the ability to build type-safe queries that can be verified by the compiler. The correctness of JPQL queries cannot be verified by the compiler, and must be verified at run time during testing.
SELECT e FROM Employee e WHERE e.serviceyears < 5Here is a sample of the equivalent Criteria query:
QueryBuilder qb = emf.getQueryBuilder(); CriteriaQuery q = qb.create(Employee.class); Root e = q.from(Employee.class); q.where(qb.lt(e.get(Employee_.serviceyears), 5)); TypedQuery tq = em.createQuery(q); List result = q.getResultList();
You can read more about the Criteria API in the Apache OpenJPA User Guide.