Criteria queries in EJB 3
Asked Answered
H

3

5

Can I use Criteria queries with EJB3 entities? If so, how can I combine them with EntityManager?

Horvitz answered 16/10, 2009 at 13:37 Comment(0)
M
0

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.

Medlar answered 16/10, 2009 at 13:41 Comment(1)
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".Medlar
S
7

One of the new features introduced in the JPA 2.0 is the Criteria API. You need one of the JPA2 implementations:

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();
Spurry answered 3/4, 2010 at 2:52 Comment(1)
True. But that's not part of JPA 1.0/EJB 3.0 which is what the question was about.Medlar
S
1

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.

Schmaltzy answered 6/6, 2013 at 18:1 Comment(0)
M
0

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.

Medlar answered 16/10, 2009 at 13:41 Comment(1)
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".Medlar

© 2022 - 2024 — McMap. All rights reserved.