Can I use Criteria queries with EJB3 entities? If so, how can I combine them with EntityManager?
JPA doesn't provide a Criteria API like in Hibernate. But you can use ejb3criteria, a library that provides an API based on Hibernate Criteria API design for EJB3 Persistence. ejb3criteria can be used with any EJB3 Persistence implementation.
One of the new features introduced in the JPA 2.0 is the Criteria API. You need one of the JPA2 implementations:
- Hibernate 3.5 now has JPA2 support
- EclipseLink (reference JPA2 implementation)
- Apache OpenJPA 2.0
Criteria queries are accessed through the EntityManager.getCriteriaBuilder(), and executed through the normal Query API.
EntityManager em = ...;
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();
JPA 2 (providing the criteria API) was defined in JSR 317 which can be regarded as successor to JSR 220 (the original EJB/JPA specification). Hence your comment "True. But that's not part of JPA 1.0/EJB 3.0 which is what the question was about." is irrelevant as you can use JPA 2 interchangeably on all common application servers (WebLogic, JBOSS, Glassfish, etc.) Green field projects will use JPA 2.0 or later. You will find many projects implemented using JPA 1 but most companies are in the process of replacing the JPA 1 framework.
JPA doesn't provide a Criteria API like in Hibernate. But you can use ejb3criteria, a library that provides an API based on Hibernate Criteria API design for EJB3 Persistence. ejb3criteria can be used with any EJB3 Persistence implementation.
© 2022 - 2024 — McMap. All rights reserved.