JPA Repository nativeQuery with pageable, join table 在 pageable.size = 1 时不工作
JPA Repository nativeQuery with pageable, join table not working when pageable.size = 1
我有这个 sql 查询很好用
SELECT ppd.userId, AVG(Coalesce(sm.score, 0)) AS avgScore
FROM Table1 ppd
LEFT JOIN Table2 sm ON ppd.userId = sm.userId AND sm.created BETWEEN start AND end
WHERE ppd.someId IN (listOfIds)
GROUP BY ppd.userId ORDER BY avgScore DESC LIMIT 1 OFFSET 0;
我在 JPARepository 中编写了一个方法查询,如下所示
@Query(value = "SELECT ppd.userId, AVG(Coalesce(sm.score, 0)) AS avgScore " +
" FROM Table1 ppd " +
" LEFT JOIN Table2 sm ON sm.userId = ppd.userId AND sm.created BETWEEN :start AND :end " +
" WHERE ppd.someId IN (:IdList) GROUP BY ppd.userId",
nativeQuery = true)
Page<MyDtoProjection> getAvg(@Param("IdList") List<String> IdList,
@Param("start") Long start,
@Param("end") Long end,
Pageable pageable);
除了当可分页大小为 1 时(SQL 限制为 1 的查询工作正常),它按预期工作。
对于可分页参数,服务发送以下可分页对象
Sort sort = JpaSort.unsafe(Sort.Direction.ASC, "(avgScore)");
Pageable pageable = PageRequest.of(offset, limit, sort);
请求设置offset=0和limit=1
我从使用存储库代码的请求中收到的错误是:
Unknown column 'ppd' in 'field list'\nQuery is: select count(ppd)...
,我不知道为什么它以别名ppd作为列名,另外,我从另一个post上读到,这种执行本机查询的方式执行计数(可以在错误消息,我没有指定那个计数查询),并且可以通过设置 countQuery 参数来指定,但我不确定在那里写什么。
解决>
刚刚将 countQuery 添加到 @Query 注释中,我不太确定在那里放什么,但这很简单
@Query(value = "SELECT ppd.userId, AVG(Coalesce(sm.score, 0)) AS avgScore " +
" FROM Table1 ppd " +
" LEFT JOIN Table2 sm ON sm.userId = ppd.userId AND sm.created BETWEEN :start AND :end " +
" WHERE ppd.someId IN (:IdList) GROUP BY ppd.userId",
countQuery = "SELECT COUNT(ppd.userId) FROM Table1 ppd " +
"WHERE ppd.userId IN (:IdList) GROUP BY ppd.userId",
nativeQuery = true)
Page<MyDtoProjection> getAvg(@Param("IdList") List<String> IdList,
@Param("start") Long start,
@Param("end") Long end,
Pageable pageable);
这是我发现的唯一 post 在其中一个答案中提到了 countQuery 及其工作原理,去看看是否有人正在经历同样的事情
Spring Data and Native Query with pagination
我有这个 sql 查询很好用
SELECT ppd.userId, AVG(Coalesce(sm.score, 0)) AS avgScore
FROM Table1 ppd
LEFT JOIN Table2 sm ON ppd.userId = sm.userId AND sm.created BETWEEN start AND end
WHERE ppd.someId IN (listOfIds)
GROUP BY ppd.userId ORDER BY avgScore DESC LIMIT 1 OFFSET 0;
我在 JPARepository 中编写了一个方法查询,如下所示
@Query(value = "SELECT ppd.userId, AVG(Coalesce(sm.score, 0)) AS avgScore " +
" FROM Table1 ppd " +
" LEFT JOIN Table2 sm ON sm.userId = ppd.userId AND sm.created BETWEEN :start AND :end " +
" WHERE ppd.someId IN (:IdList) GROUP BY ppd.userId",
nativeQuery = true)
Page<MyDtoProjection> getAvg(@Param("IdList") List<String> IdList,
@Param("start") Long start,
@Param("end") Long end,
Pageable pageable);
除了当可分页大小为 1 时(SQL 限制为 1 的查询工作正常),它按预期工作。 对于可分页参数,服务发送以下可分页对象
Sort sort = JpaSort.unsafe(Sort.Direction.ASC, "(avgScore)");
Pageable pageable = PageRequest.of(offset, limit, sort);
请求设置offset=0和limit=1
我从使用存储库代码的请求中收到的错误是:
Unknown column 'ppd' in 'field list'\nQuery is: select count(ppd)...
,我不知道为什么它以别名ppd作为列名,另外,我从另一个post上读到,这种执行本机查询的方式执行计数(可以在错误消息,我没有指定那个计数查询),并且可以通过设置 countQuery 参数来指定,但我不确定在那里写什么。
解决> 刚刚将 countQuery 添加到 @Query 注释中,我不太确定在那里放什么,但这很简单
@Query(value = "SELECT ppd.userId, AVG(Coalesce(sm.score, 0)) AS avgScore " +
" FROM Table1 ppd " +
" LEFT JOIN Table2 sm ON sm.userId = ppd.userId AND sm.created BETWEEN :start AND :end " +
" WHERE ppd.someId IN (:IdList) GROUP BY ppd.userId",
countQuery = "SELECT COUNT(ppd.userId) FROM Table1 ppd " +
"WHERE ppd.userId IN (:IdList) GROUP BY ppd.userId",
nativeQuery = true)
Page<MyDtoProjection> getAvg(@Param("IdList") List<String> IdList,
@Param("start") Long start,
@Param("end") Long end,
Pageable pageable);
这是我发现的唯一 post 在其中一个答案中提到了 countQuery 及其工作原理,去看看是否有人正在经历同样的事情 Spring Data and Native Query with pagination