Can I use Criteria queries with EJB3 entities? If so, how can I combine them with EntityManager?
A:
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.
Pascal Thivent
2009-10-16 13:41:21
Thanks, it's exactly what I need! Have you ever tried it?
2009-10-16 13:58:46
Tried, yes; used in production, no. This just means I didn't use it heavily, but I'm not saying "don't use it in production".
Pascal Thivent
2009-10-16 14:14:54
+3
A:
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();
Jacek S
2010-04-03 02:52:19
True. But that's not part of JPA 1.0/EJB 3.0 which is what the question was about.
Pascal Thivent
2010-09-19 11:32:08