use*_*524 5 java mysql jpa spring-data-jpa spring-boot
使用mysql和springboot jpa,
我正在尝试使用 JPA 实现 in 子句。当参数列表传递给规范时,在编写手动查询时获得预期结果,如下所示。employeeId 是一个字符串列,既有大写字母,也有小写字母。尽管手动查询有效,但必须实施规范。
手动查询:
SELECT emp
FROM EmployeeEntitiy emp
WHERE LOWER(emp.employeeIdParam) IN(
SELECT LOWER(empRel.destinationssid)
FROM EmployeeRelationEntity empRel WHERE ((LOWER(empRel.employeeId)=:employeeIdParam
OR UPPER(empRel.employeeId)=:employeeIdParam)
OR empRel.employeeId=:employeeIdParam)
Run Code Online (Sandbox Code Playgroud)
我如何检查大写和小写的列数据,就像toPredicate重写方法中的手动查询一样。JPA规格:
private class ParentChildCISpecification implements Specification<EmployeeEntitiy> {
List<String> employeeIdParamsList = new ArrayList<String>();
public ParentChildCISpecification(List<String> employeeIdParamsList) {
this.employeeIdParamsList = employeeIdParamsList;
}
@Override
public Predicate toPredicate(Root<EmployeeEntitiy> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
return root.get("employeeId").in(employeeIdParamsList);
}
}
Run Code Online (Sandbox Code Playgroud)
传入一个小写的字符串列表并使用CriteriaBuilder.lower表达式:
@Override
public Predicate toPredicate(Root<EmployeeEntitiy> root
, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
return criteriaBuilder.lower(root.get("employeeId")).in(employeeIdParamsList);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4575 次 |
| 最近记录: |