JPA - 带有in子句和不区分大小写的规范

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JPA - 带有in子句和不区分大小写的规范相关的知识,希望对你有一定的参考价值。

使用mysql和spring boot jpa,

我正在尝试使用JPA实现in子句。当参数列表传递给规范时,在写下manaul查询时得到预期的结果如下。 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)

我如何检查资本和小的列数据,如toPredicate overriden方法中的手动查询。 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);

    }
}
答案

传入一个小写的String列表并使用CriteriaBuilder.lower表达式:

@Override
    public Predicate toPredicate(Root<EmployeeEntitiy> root
        , CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {

        return criteriaBuilder.lower(root.get("employeeId")).in(employeeIdParamsList);

    }
另一答案

SHOW CREATE TABLE EmployeeEntitiy - 你可能会发现COLLATION...bin而不是...ci(用于“Case Insensitive”)

以上是关于JPA - 带有in子句和不区分大小写的规范的主要内容,如果未能解决你的问题,请参考以下文章