Spring - 无法将 Pageable 设置为小于总结果大小

Spring - Can't set Pageable to less than total result size

我有一个奇怪的情况,如果我将页面大小设置为小于结果的总大小,它会出错。

在我的 repo 中,有一个包含在 WITH 中的巨大查询以保持简单,因此应该在那里进行分页(我还重命名了引号内的一些字段,除非嵌套,否则 jpa 无法找到它们),像这样:

回购:

String QRY_DATA = "WITH result AS (<ton of code>) SELECT * FROM result";

@Query(nativeQuery = true, value = QRY_DATA)
Page<IBusinessDataDto> getData(UUID userId, Pageable pageable);

服务:

...
Pageable pageable = PageRequest.of(page - 1, pageSize, Sort.Direction.ASC, "businessId");
Page<IBusinessDataDto> test = repository.getData(userId, pageable);
...

我已经在代码之外测试了查询,它工作正常并且有 returns 2 个结果,如果我将 'pageSize' 设置为 3,它在代码中工作得很好,但是如果我设置它到 2 或 1,我得到一个错误:

org.postgresql.util.PSQLException: ERROR: syntax error at or near "FROM"

我尝试打印飞行路线 SQL,我得到以下信息:

Hibernate: WITH result AS (...) SELECT * FROM result order by businessId asc limit ?
Hibernate: WITH result AS (...) SELECT * FROM result
2021-11-11 15:58:48.108  WARN 17088 --- [nio-8091-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42601
2021-11-11 15:58:48.108 ERROR 17088 --- [nio-8091-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: syntax error at or near "FROM"
  Position: 1006

我不知道为什么这会很重要或有什么问题,请指教。

分页通过发出计数查询来工作,您的代码中缺少此查询。

String QRY_DATA = "WITH result AS (<ton of code>) SELECT * FROM result";
String QRY_DATA_COUNT = "WITH result AS (<ton of code>) SELECT COUNT(*) FROM result";

@Query(nativeQuery = true, value = QRY_DATA, countQuery = QRY_DATA_COUNT)
Page<IBusinessDataDto> getData(UUID userId, Pageable pageable);

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query.native