为了账号安全,请及时绑定邮箱和手机立即绑定

使用“可分页”进行排序时出现重复项

使用“可分页”进行排序时出现重复项

沧海一幻觉 2022-09-14 10:43:27
控制器:@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);    }服务:@Entity@Data@Table(name = "service")public class Service {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    @Column(unique = true, nullable = false, columnDefinition = "serial")    private Long id;    @Column(name = "name", nullable = false)    private String name;    @Column(name = "price", nullable = false)    private float price;    @Column(name = "season", nullable = false, columnDefinition = "season_enum")    @Enumerated(EnumType.STRING)    @Type(type = "pgsql_enum")    private SeasonEnum season;    )   }一个服务可以有许多作业。我试图实现的是按作业中的第一个项目对 ASC/DESC 进行排序,但目前,如果服务有 4 个作业,则在排序后使用以下方法进行排序:localhost:8080/serviceslist?sort=jobs&sortDir=asc它给了我4次服务,但我需要它与众不同。可分页是否有办法删除重复项并解决我的排序问题?按名称和东西排序工作正常,只是工作部分坏了:/编辑21.04.2019:如果我之前不太清楚,我很抱歉。理想的结果是按字母顺序对第一个作业进行排序,因为作业是按完成时间的顺序排列的。这有什么可能吗?提前致谢!
查看完整描述

3 回答

?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

您必须设置不同的,并检查查询返回类型:


将调用长分页计数查询


Specification<com.bitweb.syda.data.entity.service.Service> spec = (root, cq, cb) -> {

    if(!Long.class.isAssignableFrom(cq.getResultType())) {

        cq.distinct(true);

    }

    //else {

         //create a query for count case if needed 

    //}

    return null;

};

编辑的答案:


在这种情况下,考虑到 updateTime 可能指示作业排序,我建议您执行以下操作:


Specification<com.bitweb.syda.data.entity.service.Service> spec = (root, cq, cb) -> {

    if(!Long.class.isAssignableFrom(cq.getResultType())) {


        if(sort.contains("jobs")) {

            Join<Service, Job> jobs = root.join("jobs");

            //check for asc or desc

            cq.orderBy(cb.asc(jobs.get("updateTime")));

        }

        cq.distinct(true);

    }

    //else {

         //create a query for count case if needed 

    //}

    return null;

};

干杯


查看完整回答
反对 回复 2022-09-14
?
红颜莎娜

TA贡献1842条经验 获得超12个赞

您需要有一个唯一的订单标准。如果不确定,请添加“id”作为最低优先级的排序条件。

问题是:如果未指定到记录级别,则数据库排序未定义。即:如果您指定排序但保留最后一位未定义(即,如果两行满足相同的排序条件),您将遇到即使一行中的两个相同查询也会返回相同的结果,但顺序不同。


查看完整回答
反对 回复 2022-09-14
?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

你可以做这样的事情


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 distinc 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);

    }


查看完整回答
反对 回复 2022-09-14
  • 3 回答
  • 0 关注
  • 70 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号