条件 API。带 OrderBy 表达式的谓词

Criteria API. Predicate with OrderBy expression

我有自己的 Specification 实现的回购协议,其中 toPredicate 方法作为主要查询构造,我尝试添加 order by 表达式:

public Predicate toPredicate(@NotNull Root<Strategy> root,
                             @NotNull CriteriaQuery<?> query,
                             @NotNull CriteriaBuilder builder) {
     Predicate predicate = builder.conjunction();
     List<Expression<Boolean>> exps = predicate.getExpressions();

     ... adding different expressions to exps.add(...)

     // I get an id for descending sort due to Postgres just increment it.
     Order orderBy = builder.desc(root.get("id"));
     Expression<?> expression = orderBy.getExpression();

     // Problem here.
     exps.add(expression);
     return predicate;
}

来自 orderBy.getExpression() 的表达式是 <?> 通用的,但原始表达式列表需要 <Boolean> 类型。如何连接它们?

Specification 仅用于编码 where-clauses。如果您想对结果进行排序,请使用 Sort instance as an additional parameter.

分页排序意味着排序应该是 Pageable 的字段,如下所示:

Pageable pagination = PageRequest.of(pageNumber, pageSize, Sort.by("id").descending());