一尘不染

即使我指定了计数查询,也无法将本机查询与动态排序和/或分页一起使用

hibernate

@Query(value =“从owner_null或owner =?1的paper_entry中选择,countQuery
=”从owner_null不为null或owner =?1的paper_entry中选择count(
),nativeQuery = true)

页面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)at
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] at ........


阅读 221

收藏
2020-06-20

共1个答案

一尘不染

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