JPA Criteria builder IN clause query

We Are Going To Discuss About JPA Criteria builder IN clause query. So lets Start this Java Article.

JPA Criteria builder IN clause query

Advertisements
  1. 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);

  2. 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

Advertisements

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

Advertisements

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

Advertisements
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

Advertisements

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.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment