springdatajpa 分页pageable 不能用呢

为了那份宁静 2016-06-17 04:38:03
今天试了一下springdatajpa 的带查询参数的分页功能,如果像官网给出的例子,使用本地sql 就报错,官方的例子如下
Note, that we currently don’t support execution of dynamic sorting for native queries as we’d have to manipulate the actual query declared and we cannot do this reliably for native SQL. You can however use native queries for pagination by specifying the count query yourself:

Example 50. Declare native count queries for pagination at the query method using @Query
public interface UserRepository extends JpaRepository<User, Long> {

@Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1",
countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
nativeQuery = true)
Page<User> findByLastname(String lastname, Pageable pageable);
}
This also works with named native queries by adding the suffix .count to a copy of your query. Be aware that you probably must register a result set mapping for your count query, though.


我本地是这样写的。
@Query(value = "select u from UserInfo u where u.name=?1",
countQuery = "select count(*) from UserInfo u where u.name=?1",
nativeQuery = true)
Page<UserInfo> findByNameLike(String name,Pageable pageable);

运行就出错了。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appliation': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.it.core.repository.UserRepository com.it.core.web.Appliation.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is org.springframework.data.jpa.repository.query.InvalidJpaQueryMethodException: Cannot use native queries with dynamic sorting and/or pagination in method public abstract org.springframework.data.domain.Page com.it.core.repository.UserRepository.findByNameLike(java.lang.String,org.springframework.data.domain.Pageable)
...全文
3156 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
duwujiejie 2018-11-30
  • 打赏
  • 举报
回复
引用 10 楼 子非鱼yy 的回复:
改成这样试试: @Query(value = "select u from UserInfo u where u.name=?1 ORDER BY ?#{#pageable}", countQuery = "select count(*) from UserInfo u where u.name=?1", nativeQuery = true) Page<UserInfo> findByNameLike(String name,Pageable pageable);
赞,试了一下是可以的,在老版本的jpa可以这么用 ORDER BY ?#{#pageable}
gundongdexueqiu 2017-12-07
  • 打赏
  • 举报
回复
我用的:/*#pageable*/ 参考:https://stackoverflow.com/questions/38349930/spring-data-and-native-query-with-pagination
子非鱼yy 2017-10-20
  • 打赏
  • 举报
回复
改成这样试试: @Query(value = "select u from UserInfo u where u.name=?1 ORDER BY ?#{#pageable}", countQuery = "select count(*) from UserInfo u where u.name=?1", nativeQuery = true) Page<UserInfo> findByNameLike(String name,Pageable pageable);
lucky121 2017-09-28
  • 打赏
  • 举报
回复
SELECT * FROM USERS WHERE LASTNAME = ?1 \n-- #pageable\n
meshowmeshow 2016-10-10
  • 打赏
  • 举报
回复
引用 6 楼 linhaiguo 的回复:
官方不支持这样的写发,只能用别的方式了。


引用 7 楼 baishalinkong 的回复:
nativeQuery = true去掉,这种不能使用本地的


官网文档说 从 1.9.5 以后支持这种用法,



看清楚是 原生sql nativeQuery. ,不过我用到项目里也是报这个错,郁闷,这文档写的歌啥
baishalinkong 2016-08-08
  • 打赏
  • 举报
回复
nativeQuery = true去掉,这种不能使用本地的
为了那份宁静 2016-06-20
  • 打赏
  • 举报
回复
官方不支持这样的写发,只能用别的方式了。
为了那份宁静 2016-06-17
  • 打赏
  • 举报
回复
补充一下,sql 语句是这样的,上面的可能不对,我是使用原始的sql @Query(value = "select * from userinfo u where u.name=?1", countQuery = "select count(*) from userinfo u where u.name=?1", nativeQuery = true) Page<UserInfo> findByNameLike(String name,Pageable pageable);
为了那份宁静 2016-06-17
  • 打赏
  • 举报
回复
引用 3 楼 qq_15915835 的回复:
select u from UserInfo u where u.name=?1 改成select * 呢。
不行,我一开始是用的*
qq_15915835 2016-06-17
  • 打赏
  • 举报
回复
select u from UserInfo u where u.name=?1 改成select * 呢。
为了那份宁静 2016-06-17
  • 打赏
  • 举报
回复
错误我贴出来看得比较舒服一点。

为了那份宁静 2016-06-17
  • 打赏
  • 举报
回复 1
官方地址如下:http://docs.spring.io/spring-data/jpa/docs/1.10.2.RELEASE/reference/html/

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧