繁体   English   中英

Spring Data Hibernate + Pageable:返回空结果

[英]Spring Data Hibernate + Pageable: Returns empty results

我正在使用Spring Data存储库,没有任何问题。 当我尝试添加分页(使用Pageable界面)时,它工作正常。

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

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

new PageRequest(pageIndex_, objectsPerPage_, new Sort(orders))

当它与返回少于10个结果的查询一起使用时,结果列表为空。

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

repository.findAll(MySpecification.searchClients(criteria),
            myPagingSpecification(criteria.getPageIndex(), criteria.getNumberPerPage(), null))
            .getContent();

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

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

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

根据PageRequest实现:

public int getOffset() {
    return page * size;
}

因此,如果将page设置为0offset值也必须为0 ,并且不能大于total (如果total > 0 )。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM