我正在尝试按照处于ManyToMany关系中的Jobs对服务进行排序。
public Page<com.bitweb.syda.data.entity.service.Service> getServicesList(ServiceListRequest request, Pageable pageable, String sort) {
Specification<com.bitweb.syda.data.entity.service.Service> spec = (root, cq, cb) -> {
if(!Long.class.isAssignableFrom(cq.getResultType())) {
if(sort.contains("jobs")) {
Join<com.bitweb.syda.data.entity.service.Service, Job> jobs = root.join("jobs");
//check for asc or desc
cq.groupBy(root.get("id"), jobs.get("id"));
cq.orderBy(cb.asc(jobs.get("name")));
}
}
return null;
};
if (request.getSearch() != null) spec = spec.and(search(request.getSearch()));
if (request.getName() != null) spec = spec.and(name(request.getName()));
if (request.getJobs() != null) spec = spec.and(hasJobs(request.getJobs()));
if (request.getNeedsPatrol() != null) spec = spec.and(needsPatrol(request.getNeedsPatrol()));
return serviceRepository.findAll(spec, pageable);
}
我得到的错误是
ERROR: column "job4_.id" must appear in the GROUP BY clause or be used in an aggregate function
从postgre日志中,我可以看到查询结束,如下所示:
group by service0_.id , job2_.id order by job4_.id asc
我怎样才能使它正常工作?