JPA Criteria builder IN clause query

前端 未结 5 1755
花落未央
花落未央 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:04

    Not to break your head when you need to do this again using Criteria API - you can create a parent wrapper class for your Dao and put a hepler method there:

    /**
      * An SQL "WHERE IN" expression alternative.
      *
      * @param inList List to search in.
      * @param pathToEntityField Path to a field in your entity object.
      * @return A ready predicate-condition to paste into CriteriaQuery.where().
      */
     @SuppressWarnings("unchecked")
     private Predicate in(List inList, Expression pathToEntityField) {
    
       return pathToEntityField.in(inList);
    }
    

    Use WHERE IN then like:

    query.where(**in**(myList, root.get(MyTypedEntity_.id)));
    

提交回复
热议问题