当使用spring data jpa 的@query简单查询时,需要在HQL 中使用 join
Repository中的方法
new Specification<T>() {
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
//话说这方法找了好久好久...如果用寻常path,亦会抛异常
Join join = root.join(root.getModel().getList("productStores", ProductStore.class),JoinType.LEFT);
return builder.equeal(join.get("id"),<仓库id>)
}
};
关联关系一对多下面是分页查询
try {
page = taskWorkRepository.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList();
if (StringUtils.isNotBlank(status)) {
Join<EcTaskEntity, TaskWorkEntity> join = root.join("ecTaskEntity", JoinType.LEFT);
predicates.add(cb.equal(join.get("status"), status));
}
if (StringUtils.isNotBlank(serial)) {
predicates.add(cb.like(root.get("serial"), "%" + serial + "%"));
}
predicates.add(cb.equal(root.get("isDelete"), 0));
Predicate[] arrayPredicates = new Predicate[predicates.size()];
return cb.and(predicates.toArray(arrayPredicates));
}, pageable);
} catch (Exception e) { }