Java Reference
In-Depth Information
The Criteria API
JPA since Version 2.0 offers two options for defining queries on entities. The first op-
tion is the JPQL which is a query language based on SQL. The second option is the
Criteria API where a query is constructed essentially with Java objects, as shown in
the following code:
EntityManagerFactory emf
EntityManager em = emf.createEntityManager();
//create entity manager
//criteria builder declaration
CriteriaBuilder cb = em.getCriteriaBuilder();
//declaration of the object that will be
returned by the query
CriteriaQuery<Student> cq =
//Declaration of the entity to which the
request is made
Root<Student> student = cq.from(Student.class);
//Query construction<String>get("id"),
TypedQuery<Student> tq = em.createQuery(cq);
//execution of the query
System.out.println("result :
//JPQL equivalent query
SELECT a FROM Student a WHERE > 123
Given that the two solutions do not evolve at the same rate, the major changes in the
Criteria API are support for bulk update/delete and new reserved identifiers.
Search WWH ::

Custom Search