SpringData JPA @Query分页查询Exception问题解决方案

Caused by: 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
     at org.springframework.data.jpa.repository.query.NativeJpaQuery.(NativeJpaQuery.java: 55 )
     at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java: 72 )
     at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java: 53 )
     at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java: 144 )
     at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java: 212 )
     at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java: 77 )
     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.(RepositoryFactorySupport.java: 435 )     at org.springframework.data.repository.core.support.RepositoryFactorySupport.g

产生如上错误的语句如下:
@Query(value = "select * from chatrooms order by ctime asc"
        ,countQuery = "select count(*) from chatrooms",nativeQuery = true)
正确语句
@Query(value = "select * from chatrooms order by ctime asc /*#{pageable}*/"
        ,countQuery = "select count(*) from chatrooms",nativeQuery = true)

Spring官网给出的栗子
SpringData JPA @Query分页查询Exception问题解决方案_第1张图片

参考解决方案 :http://stackoverflow.com/questions/38349930/spring-data-and-native-query-with-pagination




你可能感兴趣的:(Java随记)