JPA Criteria builder IN clause query

前端 未结 5 1713
花落未央
花落未央 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 07:57

    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.

    0 讨论(0)
  • 2020-11-30 07:58
    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

    0 讨论(0)
  • 2020-11-30 08:03

    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.

    0 讨论(0)
  • 2020-11-30 08:04

    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;
    }
    
    0 讨论(0)
  • 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)));
    
    0 讨论(0)
提交回复
热议问题