java – 即使我已经指定了count查询,也不能使用动态排序和/或分页的本机查询

@Query(
value =“select * from paper_entry,其中owner为null或owner =?1”,
countQuery =“从paper_entry中选择count(*),其中owner是null或owner =?1”,
nativeQuery = true)

Page findAll(Long userId,Pageable pageable);

我使用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.但是我收到了这个错误.

引起:org.springframework.data.jpa.repository.query.InvalidJpaQueryMethodException:不能在方法public abstract 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]
    在 ……..

最佳答案
解决没? 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);

这是question的重复.

转载注明原文:java – 即使我已经指定了count查询,也不能使用动态排序和/或分页的本机查询 - 代码日志