JPA Criteria builder IN clause query

前端 未结 5 1757
花落未央
花落未央 2020-11-30 07:20

How to write criteria builder api query for below given JPQL query? I am using JPA 2.2.

SELECT * 
FROM Employee e
WHERE e.Parent IN (\'John\',\'         


        
5条回答
  •  伪装坚强ぢ
    2020-11-30 08:03

    You can also do this with Criteria API In clause as below:

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(Employee.class);
    Root root = cq.from(Employee.class);
    List parentList = Arrays.asList("John", "Raj");
    In 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.

提交回复
热议问题