【问题标题】:Spring Jpa Specification Join table with likeSpring Jpa Specification Join table with like
【发布时间】:2017-12-13 04:53:15
【问题描述】:

我想使用spring JPA规范的join函数

这是我的表格代码:

  1. 表学生
  2. 父级表

    public static Specification<InStudent> filterByKeywordAndStatus(final String keyword) {
    return (Root<InStudent> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
        List<Predicate> predicates = new ArrayList<>();
    
        if (StringUtils.hasText(keyword)) {
    
            predicates.add(
                    cb.or(
                            cb.like(root.get(InStudent_.name), "%" + keyword + "%"),
                            cb.like(root.get(InStudent_.address), "%" + keyword + "%"),
                            cb.like(root.get(InStudent_.phone), "%" + keyword + "%")
                    )
            );
        }
    
        return cb.and(predicates.toArray(new Predicate[predicates.size()]));
    };
    

    }

如何在规范中加入表 inStudent 和 inParent?

【问题讨论】:

  • 您没有在代码中包含关系。无论如何使用连接就像Join&lt;InStudent, InParent&gt; parent = root.join(InStudent_.parent); 假设指定关系的字段是parent
  • 您好,如果回答对您有帮助,别忘了接受/点赞。

标签: java jpa spring-boot spring-data-jpa


【解决方案1】:

试试这样的(如果我理解正确的话):

public static Specification<Student> filterByKeywordAndStatus(String keyword, String parentStatus) {

    return (student, query, cb) -> {

        Join<Student, Parent> joinParent = student.join("parent");

        return cb.and(
                cb.or(
                   cb.like(student.get("name"), "%" + keyword + "%"),
                   cb.like(student.get("address"), "%" + keyword + "%"),
                   cb.like(student.get("phone"), "%" + keyword + "%")
                ),
                cb.like(joinParent.get("status"), "%" + parentStatus + "%"));
    };
}

【讨论】:

    猜你喜欢
    • 2018-05-13
    • 2021-12-19
    • 2021-02-27
    • 2017-08-21
    • 2022-10-06
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多