spring - 带规范的可分页排序返回重复的结果
问题描述
我有一个 Service 实体,它有一个 Jobs 字段,它在 ManyToMany 关系中有 Job 实体。
过滤、排序和分页工作完美,但唯一的问题是按作业排序。
当我向我发送请求时,/serviceslist?sort=jobs&sortDir=asc
我会为每个连接了多个作业的服务获得重复的结果。更具体地说,如果一个 Service 有 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);
而不是 distinct 时,我收到此错误:
PSQLException: ERROR: column "job4_.id" must appear in the GROUP BY clause or be used in an aggregate function
Position: 780
解决方案
推荐阅读
- javascript - 未处理的承诺拒绝:DiscordAPIError:无法向该用户发送消息
- python - 如何将此数字四舍五入到最接近的 10,000 位?
- node.js - 使用 express node.js 更新数据时出现错误
- php - git clone 后 PHP 工匠和作曲家不工作
- javascript - 无法使用 $.ajax({ type: "POST", using VS2017 C# 将数据从 aspx 文件发送到代码后面
- php - 如何解决这个闭包对象在工厂中不能有属性
- julia - ModelingToolkit(MTK)中是否有类似modelica的homopy()的算子?
- python-3.x - 获取 ValueError:as_list() 未在未知的 TensorShape 上定义。但 as_list() 在 (next(iter(dataset))) 上工作正常
- r - 如何为我的数据类型使用 Facet Grid
- python - 如何在 python 中使用多个分类器在 KFold 中绘制混淆矩阵?