java 即使我指定了计数查询,也不能使用带有动态排序和/或分页的本机查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44060795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:54:40  来源:igfitidea点击:

Cannot use native queries with dynamic sorting and/or pagination even if I have specified the count query

javamysqlspringhibernatespring-data-jpa

提问by JiaLun Li

@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)

@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);

页面 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.

我使用 mysql 5.7,spring-data-jpa 1.11.3.RELEASE。如您所见,我遵循文档https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query。但是我收到了这个错误。

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 ........

引起:org.springframework.data.jpa.repository.query.InvalidJpaQueryMethodException:无法在方法公共抽象org.springframework.data.domain.Page com.gbdata.entry.persistence.dao中使用带有动态排序和/或分页的本机查询.PaperEntryRepository.findAll(java.lang.Long,org.springframework.data.domain.Pageable) 在 org.springframework.data.jpa.repository.query.NativeJpaQuery.(NativeJpaQuery.java:55) ~[spring-data-jpa -1.11.3.RELEASE.jar:na] 在 org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:72) ~[spring-data-jpa-1.11.3.RELEASE.jar: na] 在…………

回答by zhouji

解决没? SQL里面加 ORDER BY ?#{#pageable}就可以了

解决没有?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.

这是这个问题的副本。