繁体   English   中英

如何使用带有Spring数据的Querydsl谓词(BooleanExpression)按计算字段进行过滤?

[英]How to filter by calculated field using Querydsl Predicate (BooleanExpression) with Spring Data?

数据库中有两个数字列,例如实际和计划。

我可以像差异一样添加第三列,并通过此列编写查询过滤的WHERE部分。

但是,我想在Predicate中计算它,而不是在数据库中有其他列。

像(qProduct.actualPrice-qProduct.planPrice).gt(20L)之类的东西

实体:

public class Product {
    private String type;
    private Long actualPrice;
    private Long planPrice;
}

谓语:

QProduct qProduct = QProduct.product;
BooleanExpression predicate = qProduct.type.eq("pizza")
            .and((qProduct.actualPrice - qProduct.planPrice).gt(20L));

Page<Product> productPage = productRepository.findAll(predicate, pageable);

谢谢

您可以使用NumberExpression类的减法

QProduct qProduct = QProduct.product;
BooleanExpression predicate = qProduct.type.eq("pizza")
          .and((qProduct.actualPrice.subtract(qProduct.planPrice)).gt(20L));

Page<Product> productPage = productRepository.findAll(predicate, pageable);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM