我想使用spring JPA规范连接函数
这是我的表格代码:
表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()]));
};
}
如何在规范中加入表inStudent和inParent?
答案 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 + "%"));
};
}