简体   繁体   English

JPA Criteria builder IN 子句查询

[英]JPA Criteria builder IN clause query

How to write criteria builder api query for below given JPQL query?如何为下面给定的 JPQL 查询编写标准构建器 api 查询? I am using JPA 2.2 .我正在使用JPA 2.2

SELECT * 
FROM Employee e
WHERE e.Parent IN ('John','Raj')
ORDER BY e.Parent

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.我在这里使用了重载的CriteriaQuery.where方法,在这种情况下它接受一个Predicate .. in谓词。

You can also do this with Criteria API In clause as below:您也可以使用 Criteria API In 子句执行此操作,如下所示:

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.查看我的Github以了解这个和几乎所有可能的标准示例。

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.有关更多信息,请参阅 baeldung 的这篇文章。 it is very resourceful https://www.baeldung.com/jpa-criteria-api-in-expressions它非常足智多谋https://www.baeldung.com/jpa-criteria-api-in-expressions

I hope it could be useful.我希望它可能有用。 The code tested in case when Entry has only one @Id column:在 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;
}

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:当您需要使用 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);
}

Use WHERE IN then like:使用 WHERE IN 然后像:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM