New or updated for this feature pack


Criteria API

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 offers a means for 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.

The following is a sample JPQL query that returns a list of employees with less than five years of service:
SELECT e FROM Employee e WHERE e.serviceyears < 5
Here 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();
Note: Employee_ is the Metamodel of the Employee class.
Two important features are improvements from JPQL:

The Criteria API is covered in the Chapter 11 of the OpenJPA manual.




Subtopics
New or updated for this feature pack Developing JPA 2.x applications for a Java SE environment
New or updated for this feature pack Developing JPA 2.x applications for a Java EE environment
Related reference
Related information
New or updated for this feature pack Dynamic, typesafe queries in JPA 2.0
Reference topic Reference topic    

Terms and conditions for information centers | Feedback

Last updatedLast updated: Jan 16, 2013 3:37:33 AM CST
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=v700osgijpa&product=was-nd-mp&topic=rejb_criteriaapi
File name: rejb_criteriaapi.html