弹簧数据-通过列乘法进行排序 [英] Spring data - Order by multiplication of columns

查看:107
本文介绍了弹簧数据-通过列乘法进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I came to a problem where I need to put ordering by multiplication of two columns of entity, for the sake of imagination entity is:

@Entity
public class Entity {

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

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

    .
    .
    .
    many more columns
}

我的repo接口实现了JpaRepository和QuerydslPredicateExecutor, 但我正在努力寻找一种通过金额*单价"对数据进行排序的方法, 因为我找不到将它放入其中的方法

My repo interface implements JpaRepository and QuerydslPredicateExecutor, but I am struggling to find a way to order my data by "amount*unitPrice", as I can't find a way to put it into

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

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

没有PropertyReferenceException:没有属性数量*抛出单价....

without having PropertyReferenceException: No property amount * unitPrice... thrown.

我无法使用用户命名查询,因为我的查询需要根据用户输入进行大量过滤(无法将where子句放入查询中,因为如果用户未选择任何值,那么where子句就不能位于查询).

I can't user named query, as my query takes quite massive filter based on user inputs (can't put where clause into query, because if user hasn't selected any value, where clause can't just be in query).

为了简单起见.我需要诸如findAll(Predicate,Pageable)之类的东西,但我需要强制该查询按金额*单价"对其自身进行排序,同时还要保留我的Preditate(过滤器)和Pageable(偏移量,限制,其他排序). >

To make it simple. I need something like findAll(Predicate, Pageable), but I need to force that query to order itself by "amount * unitPrice", but also have my Preditate (filter) and Pageable (offset, limit, other sortings) untouched.

推荐答案

Spring Sort只能 用于按属性而不是表达式进行排序.

Spring Sort can be used only for sorting by properties, not by expressions.

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

But you can create a unique sort in a Predicate, so you can add this sort-predicate to your other one before you call the findAll method.

这篇关于弹簧数据-通过列乘法进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆