Raj*_*Raj 26 java orm jpa openjpa jpa-2.0
如何为下面给出的JPQL查询编写条件生成器api查询?我在用JPA 2.2.
SELECT *
FROM Employee e
WHERE e.Parent IN ('John','Raj')
ORDER BY e.Parent
Run Code Online (Sandbox Code Playgroud)
Mac*_*ski 47
这个标准设置应该可以解决问题:
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();
Run Code Online (Sandbox Code Playgroud)
我已经使用了重载CriteriaQuery.where此处方法,该方法接受Predicate..一个in在此情况下断言.
Van*_*ria 11
您也可以使用 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();
Run Code Online (Sandbox Code Playgroud)
查看我的Github以了解这个和几乎所有可能的标准示例。
小智 5
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));
Run Code Online (Sandbox Code Playgroud)
这应该可以正常工作。有关更多信息,请参阅 baeldung 的这篇文章。它非常足智多谋https://www.baeldung.com/jpa-criteria-api-in-expressions
| 归档时间: |
|
| 查看次数: |
40683 次 |
| 最近记录: |