Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/Examples/JPA/2.0/Criteria

< EclipseLink‎ | Examples‎ | JPA
Revision as of 11:47, 30 November 2009 by James.sutherland.oracle.com (Talk | contribs) (New page: ===Criteria API (JPA 2.0)=== JPA 2.0 defines a query <i>criteria</i> API to simplify dynamic query creation. With JPQL dynamic queries would require performing string concatenations to bu...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Criteria API (JPA 2.0)

JPA 2.0 defines a query criteria API to simplify dynamic query creation. With JPQL dynamic queries would require performing string concatenations to build queries dynamically from web forms or dynamic content. JPQL is also not checked until runtime, making typos more common. The criteria API using a set of Java interfaces to allow queries to be dynamically constructed, and reduces runtime typos with compile time checking.

Criteria queries are accessed through the EntityManager.getCriteriaBuilder() API, and executed through the normal Query API.

Criteria queries can use parameters, and query hints the same as named queries.

Example dynamic query execution

EntityManager em = getEntityManager();
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Employee> query = qb.createQuery(Employee.class);
Root<Employee> employee = query.from(Employee.class);
query.where(qb.equal(employee.get("firstName"), "Bob"));
List<Employee> result = em.createQuery(query).getResultList();
...

Back to the top