Spring数据 - 按列乘法排序

问题描述 投票:1回答:1

我遇到了一个问题,我需要通过乘以两列实体来排序,为了想象,实体是:

@Entity
public class Entity {

    @Column(name="amount")
    private BigDecimal amount;

    @Column(name="unitPprice")
    private BigDecimal unitPrice;

    .
    .
    .
    many more columns
}

我的repo接口实现了JpaRepository和QuerydslPredicateExecutor,但是我很难找到一种通过“amount * unitPrice”来命令我的数据的方法,因为我找不到将它放入的方法

PageRequest(new Sort.Order(ASC, "amount * unitPrice")

没有PropertyReferenceException:没有属性数量* unitPrice ...抛出。

我不能用户命名查询,因为我的查询根据用户输入采用了相当大的过滤器(不能将where子句放入查询中,因为如果用户没有选择任何值,where子句不能只是在查询中)。

简单一点。我需要像findAll(Predicate,Pageable)这样的东西,但是我需要强制查询按“amount * unitPrice”排序,但也要保持我的Preditate(过滤器)和Pageable(偏移,限制,其他排序)。

spring jpa spring-data-jpa
1个回答
0
投票

Spring Sort只能用于按属性排序,而不能用于表达式。

但是您可以在Predicate中创建一个唯一的排序,因此您可以在调用findAll方法之前将此排序谓词添加到另一个排序谓词中。

© www.soinside.com 2019 - 2024. All rights reserved.