Spring Jpa规范加入表格

时间:2017-07-09 10:26:50

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

我想使用spring JPA规范连接函数

这是我的表格代码:

  1. 表InStudent
  2. 表InParent

    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()]));
    };
    

    }

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

1 个答案:

答案 0 :(得分:2)

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

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 + "%"));
    };
}