簡體   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