Spring Data Hibernate + Pageable:返回空结果

m.m*_*sky 8 java spring hibernate spring-data

我正在使用Spring Data存储库而没有任何问题.当我尝试添加Paging(使用Pageable接口)时,它工作正常.

但是,当返回的结果集小于Page size时,结果为空List.

以下是我的PageRequest.index和objectsPerPage的默认值分别为0和10.

new PageRequest(pageIndex_, objectsPerPage_, new Sort(orders))
Run Code Online (Sandbox Code Playgroud)

将其与返回少于10个结果的查询一起使用时,结果列表为空.

这就是我在Service层中使用存储库的方式:

repository.findAll(MySpecification.searchClients(criteria),
            myPagingSpecification(criteria.getPageIndex(), criteria.getNumberPerPage(), null))
            .getContent();
Run Code Online (Sandbox Code Playgroud)

编辑1 我找到了原因,但我仍在寻找解决方案或解决方法.

        Long total = QueryUtils.executeCountQuery(getCountQuery(spec));
    List<T> content = total > pageable.getOffset() ? query.getResultList() : Collections.<T> emptyList();
Run Code Online (Sandbox Code Playgroud)

这个代码位于SimpleJpaRepository类中select count...,如果计数小于偏移量,则返回一个空列表.

prz*_*tel 6

根据PageRequest实现:

public int getOffset() {
    return page * size;
}
Run Code Online (Sandbox Code Playgroud)

因此,如果你设置page0offset值也必须0和不能大于total(如果total > 0)。

检查(可能在调试器中)pageIndex您传递给 spring-data 的值。它可能是其他值 - 有时是简单的错误。

  • 谢谢你。问题是我的 REST 服务的 Query 参数是“page=1”,我忘记了页面是基于 0 的,但我的 API 不是。 (5认同)