java - 在 Jpa 中让服务器端分页查询聚合 sql 的最佳方法是什么?
问题描述
嗨,我想要实现的是获取 Page 的返回结果,结果进入 DTO 并作为 Pageable 请求。我该如何正确地做到这一点?
我找到了几种方式,但我不确定也不太舒服。我每次都尝试使用 QueryDsl 方式,但是当使用 sum() 和 group By 多列这样的聚合函数时,它有“计数”限制,现在将在我的存储库中编写带有 @Query() 注释的 nativeQuery,看起来像这样:
@Query(name = "find_stock_total", nativeQuery = true)
Page<StockTotalResponseDto> findStockTotalRsMap(Pageable pageable);
然后在我的实体类中,我必须编写@NamedNativeQueries 并使用@SqlResultSetMappings 对其进行映射,这是我的编写方式:
@NamedNativeQueries({
@NamedNativeQuery(
name = "find_stock_total",
query = "SELECT product_id, product_code, sum(qty) FROM "book_stock"
where warehouse_code = 'GBKTJKT1' and product_code in('MCM-508','TL-101') and branch_code = 'JKT' and branch_id = '1'
GROUP BY product_id, product_code",
resultSetMapping = "stock_total_response_dto"
),
@NamedNativeQuery(
name = "find_stock_total.count",
query = "select count(*) from (
SELECT product_id, product_code, sum(qty) FROM "book_stock"
where warehouse_code = 'GBKTJKT1' and product_code in('MCM-508','TL-101') and branch_code = 'JKT' and branch_id = '1'
GROUP BY product_id, product_code
) as count"
)
})
@SqlResultSetMappings({
@SqlResultSetMapping(
name = "stock_total_response_dto",
classes = @ConstructorResult(
targetClass = StockTotalResponseDto.class,
columns = {
@ColumnResult(name = "product_id", type = String.class),
@ColumnResult(name = "product_code", type = String.class),
@ColumnResult(name = "sum", type = BigInteger.class)
}
)
),
@SqlResultSetMapping(
name = "stock_total_response_dto.count",
columns = @ColumnResult(name = "count")
)
})
@Entity
我将不得不在 @Entity 类中编写 sql 字符串,并且我不能使用 Pageable 提供的 Sort 并且我必须编写额外的 Count 查询。我想使用 Pageable Sort 来对我的数据进行排序。有没有另一种方法可以以更好的方式或现代方式编写类似的东西?也许有人可以提供“标准”方式或者还有另一种 Jpa 方式?或者比这更好的东西?
解决方案
您应该检查Blaze-Persistence必须提供哪些工作在 JPA/Hibernate 之上并且还与 spring-data-jpa 集成,这使您可以将它用作 spring-data-jpa 正常工作的替代品.
它正确支持聚合函数(也适用于计数查询),支持键集分页等等。查看有关 Spring Data 集成的文档:https ://persistence.blazebit.com/documentation/entity-view/manual/en_US/#spring-data-integration
推荐阅读
- android - 一个子填充影响 Linearlayout 中的所有子填充
- split - 将数据拆分为批次
- cron - crontab:无效选项 -- '1' crontab:使用错误:键入 crontab -1 时无法识别选项
- python-3.x - Distribute the values of one column into N columns
- java - 如何从java运行curl(在java中使用curl命令)命令?
- node.js - 没有从 Node 应用程序中的 pg-promise 调用中得到任何响应
- vue.js - 登录时出现 Auth0 和 Nuxt JS 400(无法获取脚本源)错误
- reactjs - 是否可以在 useEffect 中为 axios 请求编写测试?
- plot - 松脚本中简单移动平均线的最后(最新)交叉
- java - 避免重载构造函数中的重复