JPA Criteria构建器IN子句查询

时间:2017-03-01 11:36:37

标签: java jpa orm jpa-2.0 openjpa

如何为下面给出的JPQL查询编写条件构建器api查询? 我正在使用SELECT * FROM Employee e WHERE e.Parent IN ('John','Raj') ORDER BY e.Parent

Derive

5 个答案:

答案 0 :(得分:39)

这个标准设置应该可以解决问题:

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();

我在这里使用了重载的CriteriaQuery.where方法,在这种情况下接受Predicate .. in谓词。

答案 1 :(得分: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));

这应该工作正常。有关更多信息,请参阅baeldung的这篇文章。 enter image description here

非常足智多谋

答案 2 :(得分:1)

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();

为此和几乎所有可能的条件示例签出我的github。

https://github.com/vaneetkataria/Jpa-Hibernate/blob/master/jdbcToJpaMigration/src/test/java/com/katariasoft/technologies/jpaHibernate/entity/criteria/SingleTableCriteriaFetchTests.java

答案 3 :(得分:0)

当您需要使用Criteria API再次执行此操作时,请不要打断您-您可以为Dao创建父包装类,然后在其中放置 hepler方法

/**
  * 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);
}

使用WHERE IN然后类似:

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

答案 4 :(得分:0)

我希望它会有用。在Entry只有一个@Id列的情况下测试了代码:

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;
}