使用Springdata Jpa 分页功能时报的错,解决办法:
在sql语句中加 ORDER BY ?#{#pageable}
就可以了
如下:
@Query(
value = "select * from paper_entry where owner is null or owner = ?1 ORDER BY ?#{#pageable}",
countQuery = "select count(*) from paper_entry where owner is not null or owner = ?1 ORDER BY ?#{#pageable}",
nativeQuery = true)
Page findAll(Long userId, Pageable pageable);
如果查询时使用的是递归查询且使用了Springdata的排序功能,如
select * from (SELECT CC.* FROM JOB_T CC START WITH CC.PRID in (?1) CONNECT BY PRIOR CC.RID= CC.PRID) CC where CC.wjlx='1' order by ?#{#pageable}
理解的三步:
1、注意别名使用,加上了排序后,?#{#pageable}运行出的语句拿的是括号内中的别名“CC” ——> order by CC.需排序字段(可自行去尝试运行,看运行后的语句)
2、故将递归后查出的结果别名设置成“CC”,使排序生效
3、若更改递归查询内JOB_T表的别名,查询结果的别名也一并更改,否则排序失效。