如何使用带有Spring Data的Querydsl Predicate(BooleanExpression)按计算字段进行过滤?

时间:2017-06-06 10:47:05

标签: java querydsl

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

我可以像差异一样添加第三列,并通过此列编写查询过滤的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);

THX

1 个答案:

答案 0 :(得分:0)

您可以使用NumberExpression class

的减法方法
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);
相关问题