We Are Going To Discuss About JPA Criteria builder IN clause query. So lets Start this Java Article.
JPA Criteria builder IN clause query
- JPA Criteria builder IN clause query
You can also do this with Criteria API In clause as below:
CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Employee> cq = cb.createQuery(Employee.class);
- JPA Criteria builder IN clause query
You can also do this with Criteria API In clause as below:
CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Employee> cq = cb.createQuery(Employee.class);
Solution 1
This criteria set-up should do the trick:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Employee> q = cb.createQuery(Employee.class);
Root<Employee> root = q.from(Employee.class);
q.select(root);
List<String> parentList = Arrays.asList(new String[]{"John", "Raj"});
Expression<String> parentExpression = root.get(Employee_.Parent);
Predicate parentPredicate = parentExpression.in(parentList);
q.where(parentPredicate);
q.orderBy(cb.asc(root.get(Employee_.Parent));
q.getResultList();
I have used the overloaded CriteriaQuery.where
method here which accepts a Predicate
.. an in
predicate in this case.
Original Author Maciej Kowalski Of This Content
Solution 2
You can also do this with Criteria API In clause as below:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Employee> cq = cb.createQuery(Employee.class);
Root<Employee> root = cq.from(Employee.class);
List<String> parentList = Arrays.asList("John", "Raj");
In<String> in = cb.in(root.get(Employee_parent));
parentList.forEach(p -> in.value(p));
return entityManager
.createQuery(cq.select(root)
.where(in).orderBy(cb.asc(root.get(Employee_.Parent)))
.getResultList();
Checkout my Github for this and almost all possible criteria examples.
Original Author Vaneet Kataria Of This Content
Solution 3
List<String> parentList = Arrays.asList("John", "Raj");
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<Employee> query = cb.createQuery(Employee.class);
final Root<Employee> employee = query.from(Employee.class);
query.select(employee).where(employee.get("Parent").in(parentList));
this should work fine. for more info please refer this article by baeldung. it is very resourceful https://www.baeldung.com/jpa-criteria-api-in-expressions
Original Author Sithija Piyuman Thewa Hettige Of This Content
Solution 4
I hope it could be useful. The code tested in case when Entry has only one @Id column:
public static <Entry, Id> List<Entry> getByIds(List<Id> ids, Class<Entry> clazz, EntityManager entityManager) throws CustomDatabaseException
{
List<Entry> entries;
try
{
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Entry> q = cb.createQuery(clazz);
Root<Entry> root = q.from(clazz);
EntityType<Entry> e = root.getModel();
CriteriaQuery<Entry> all = q.where(root.get(e.getId(e.getIdType().getJavaType()).getName()).in(ids));
TypedQuery<Entry> allQuery = entityManager.createQuery(all);
entries = allQuery.getResultList();
}
catch (NoResultException nre)
{
entries = Collections.emptyList()
}
catch (Exception e)
{
throw new CustomDatabaseException(e);
}
return entries;
}
Original Author Максим Павлючук Of This Content
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.