具有规范的可分页排序返回重复的结果

时间:2019-04-20 08:00:42

标签: spring spring-boot spring-data-jpa criteria-api

我有一个Service实体,该实体具有Jobs字段,而Jobs实体具有ManyToMany关系。

过滤,排序和分页效果很好,但是唯一的问题是按Jobs排序。

当我向/serviceslist?sort=jobs&sortDir=asc发送请求时,对于连接了多个作业的每个服务,都会得到重复的结果。更具体地说,如果一个服务有4个作业,则该服务将返回4次,依此类推。

我认为我应该使用groupBy,但是如何在下面的服务中实现groupBy?

服务:

public Page<com.bitweb.syda.data.entity.service.Service> getServicesList(ServiceListRequest request, Pageable pageable) {
        Specification<com.bitweb.syda.data.entity.service.Service> spec = where(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);
    }

控制器:

@RequestMapping(path = "/serviceslist", method = RequestMethod.GET)
    public Page<ServiceResponse> getServicesList(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String search,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String jobs,
            @RequestParam(required = false) Boolean needsPatrol,
            @RequestParam(defaultValue = "createTime") String sort,
            @RequestParam(defaultValue = "asc") String sortDir
    ) {

        ServiceListRequest request = new ServiceListRequest(search, name, jobs, needsPatrol);

        Sort.Direction direction;

        if (sortDir.equals("asc")) {
            direction = Sort.Direction.ASC;
        } else {
            direction = Sort.Direction.DESC;
        }

        return serviceService.getServicesList(request, of(page, size, direction, sort))
                .map(ServiceResponse::new);
    }

以下是我已经尝试过的一些示例:

public Page<com.bitweb.syda.data.entity.service.Service> getServicesList(ServiceListRequest request, Pageable pageable) {
        Specification<com.bitweb.syda.data.entity.service.Service> spec = (root, query, builder) -> {
            //you can do any check here if you want with the join and check all the search parameters here if you want
            //Join<Object, Object> jobs = root.join("jobs");
            // also set query to get distinct values
            query.distinct(true);
            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);
}

尝试此操作后,一切正常,但按作业排序会出现此错误:

PSQLException: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
  Position: 652

我对SQL和Criteria API不太熟练,我不知道该如何解决。

当使用query.groupBy(root);而不是不重复时,出现此错误:

PSQLException: ERROR: column "job4_.id" must appear in the GROUP BY clause or be used in an aggregate function
  Position: 780

0 个答案:

没有答案