繁体   English   中英

如何使用PathBuilder在Spring JPA + Query DSL中创建求和函数

[英]how to create Sum Function in Spring JPA + Query DSL using PathBuilder

我有如下查询

select sum(qty)... from table where productname = 'Pepsi' group by product.

下面我有创造PathBuilder QueryDSL春代码: -

PathBuilder<Stock> entityPath = new PathBuilder<Stock>(Stock.class, "stock");
BooleanPath path = entityPath.getBoolean("productname");
BooleanExpression wherecondition = path.eq("pepsi");

NumberPath<Double> path = entityPath.getNumber("qty", Double.class);
NumberExpression<Double> sumfunction= path.castToNum(Double.class).sum();

我正在使用JPA存储库来获取如下所示的分页数据:

Page<Stock> page = stockRepository.findAll(wherecondition,pageable);

我不知道如何在JPA存储库中使用上述sumfunction 因为我不能结合BooleanExpressionNumberExpression

你这样尝试过吗

@Configurable
public interface XXRepository extends PagingAndSortingRepository<xx, String> {

@Query(value = "select sum(qty)... from table where productname = ?1 group by product", nativeQuery = true)
      public String findSumOfQty(String productname);
}

xx =您代表表格的实体名称。

暂无
暂无

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

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