JPA - 对条款和大小写不敏感的特异性

使用 mysql 和弹簧靴 JPA,


我正在尝试使用JPA.在子句中实现参数列表传递给规范时,在编写如下manaul查询时获得预期结果。雇员 Id 是一个字符串列,其中既有大写字母,也有小写字母。虽然手动查询有效,但必须实现规范。


手动查询:


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)

我怎么能检查列数据与大写和小,如手动查询在到预测覆盖方法。公积管规格:


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);


    }

}


aluckdog
浏览 405回答 3
3回答

慕慕森

传入小写的字符串列表并使用表达式:CriteriaBuilder.lower@Override&nbsp; &nbsp; public Predicate toPredicate(Root<EmployeeEntitiy> root&nbsp; &nbsp; &nbsp; &nbsp; , CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {&nbsp; &nbsp; &nbsp; &nbsp; return criteriaBuilder.lower(root.get("employeeId")).in(employeeIdParamsList);&nbsp; &nbsp; }

慕盖茨4494581

在相等的情况下,可以使用以下import org.springframework.data.jpa.domain.Specification;final Specification<EmployeeEntity> spec = (employeeEntity, cq, cb) -> cb.equal(cb.lower(employeeEntity.get("email")), email.toLowerCase());final List<EmployeeEntity> empList = employeeRepository.findAll(spec, PageRequest.of(page, size));

肥皂起泡泡

做 -- 你可能会发现 是 而不是 (对于 “不区分大小写”)SHOW CREATE TABLE EmployeeEntitiyCOLLATION...bin...ci
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java