8

@Query( value = "select * from paper_entry where owner is null or owner = ?1", countQuery = "select count(*) from paper_entry where owner is not null or owner = ?1", nativeQuery = true)

Page findAll(Long userId, Pageable pageable);

I use mysql 5.7, spring-data-jpa 1.11.3.RELEASE. As you can see, I follow the document https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query. However I got this error.

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 com.gbdata.entry.persistence.dao.PaperEntryRepository.findAll(java.lang.Long,org.springframework.data.domain.Pageable) at org.springframework.data.jpa.repository.query.NativeJpaQuery.(NativeJpaQuery.java:55) ~[spring-data-jpa-1.11.3.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:72) ~[spring-data-jpa-1.11.3.RELEASE.jar:na] at ........

3
  • What's your question here? Commented May 19, 2017 at 3:15
  • take a look at this answer it may help. Commented May 19, 2017 at 3:21
  • @ControlAltDel it's all there Commented May 19, 2017 at 3:29

1 Answer 1

36

解决没? 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);

It's duplicate of this question.

Sign up to request clarification or add additional context in comments.

1 Comment

Please flag the question as a duplicate instead of posting an answer, in the future

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.