Java JPA Criteria builder IN 子句查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42530677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA Criteria builder IN clause query
提问by Raj
How to write criteria builder api query for below given JPQL query?
I am using JPA 2.2
.
如何为下面给定的 JPQL 查询编写标准构建器 api 查询?我正在使用JPA 2.2
.
SELECT *
FROM Employee e
WHERE e.Parent IN ('John','Raj')
ORDER BY e.Parent
回答by Maciej Kowalski
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
谓词。
回答by Vaneet Kataria
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 Githubfor this and almost all possible criteria examples.
查看我的Github以了解这个和几乎所有可能的标准示例。
回答by Zon
Not to break your head when you need to do this again using Criteria API - you can create a parent wrapper classfor your Dao and put a hepler methodthere:
当您需要使用 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)));
回答by Максим Павлючук
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;
}
回答by Sithija Piyuman Thewa Hettige
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
这应该可以正常工作。有关更多信息,请参阅 baeldung 的这篇文章。它非常足智多谋https://www.baeldung.com/jpa-criteria-api-in-expressions