Using the Criteria API to Create Queries
The Criteria API is used to define queries for entities and their persistent state by creating query-defining objects. Criteria queries are written using Java programming language APIs, are typesafe, and are portable. Such queries work regardless of the underlying data store.Overview of the Criteria and Metamodel APIs
Similar to JPQL, the Criteria API is based on the abstract schema of persistent entities, their relationships, and embedded objects. The Criteria API operates on this abstract schema to allow developers to find, modify, and delete persistent entities by invoking Java Persistence API entity operations. The Metamodel API works in concert with the Criteria API to model persistent entity classes for Criteria queries.The Criteria API and JPQL are closely related and are designed to allow similar operations in their queries. Developers familiar with JPQL syntax will find equivalent object-level operations in the Criteria API.
The following simple Criteria query returns all instances of the Car entity in the data source:
EntityManager em = ...; CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQueryThe equivalent JPQL query is:cq = cb.createQuery(Car.class); Root car= cq.from(Car.class); cq.select(car); TypedQuery q = em.createQuery(cq); List allCars = q.getResultList();
SELECT c FROM Car cThis query demonstrates the basic steps to create a Criteria query:
- Use an EntityManager instance to create a CriteriaBuilder object.
- Create a query object by creating an instance of the CriteriaQuery interface. This query object's attributes will be modified with the details of the query.
- Set the query root by calling the from method on the CriteriaQuery object.
- Specify what the type of the query result will be by calling the select method of the CriteriaQuery object.
- Prepare the query for execution by creating a TypedQuery
instance, specifying the type of the query result.
- Execute the query by calling the getResultList method on the TypedQuery
object. Because this query returns a collection of entities, the result is stored in a List.
To create a CriteriaBuilder instance, call the getCriteriaBuilder method on the EntityManager instance:
CriteriaBuilder cb = em.getCriteriaBuilder();The query object is created by using the CriteriaBuilder instance:
CriteriaQueryThe query will return instances of the Pet entity, so the type of the query is specified when the CriteriaQuery object is created to create a typesafe query.cq = cb.createQuery(Car.class);
The FROM clause of the query is set, and the root of the query specified, by calling the from method of the query object:
RootThe SELECT clause of the query is set by calling the select method of the query object and passing in the query root:pet = cq.from(Car.class);
cq.select(car);The query object is now used to create a TypedQuery
TypedQueryThis typed query object is executed by calling its getResultList method, because this query will return multiple entity instances. The results are stored in a Listq = em.createQuery(cq);
ListallCars = q.getResultList();
For Example:
Simple Query
Query query = entityManager.createQuery( "from SimpleBean s" ); List CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery |