簡體   English   中英

在休眠狀態下設置最大結果無法按要求工作

[英]set max results in hibernate not working as per requirement

這是我的查詢
使用HQL編寫。 我的要求如下。 我有45條記錄,我想每次獲取10條記錄。 它最多可以成功提供40條記錄。 但是從41-45條記錄中,查詢返回空列表。

query1 = session
                    .createQuery(
                            "FROM mongo c where "
                                    + "c.ad='555rs5' and "
                                    + "c.cId='44444sf' and "
                                    + "c.langId='59ecc8' and c.date < '"
                                    + tempDate + "' ORDER BY -date")
                    .setMaxResults(10); 

我的查詢有什么問題嗎? 請告訴我。
問候
納雷什·維魯里(Naresh Veluri)

對於Hibernate分頁,在setMaxResults()方法旁邊,您還需要setFirstResult()方法。 查看此頁面。

我想您的循環中肯定有問題。

根據KC的答案,您尚未設置setFirstResult()我不知道您的查詢如何獲取下一條記錄。請閱讀Hibernate Doc-setFirstResult()

Query setFirstResult(int firstResult)
Set the first row to retrieve. If not set, rows will be retrieved beginnning from row 0.
Parameters:
firstResult - a row number, numbered from 0

嘗試下面的代碼,希望它可以幫助您解決問題。

boolean flag = true;
    int firstResult = 0;
    int pageSize = 10;
    int maxResult = 10;

    while(flag) {
        query1 = session.createQuery("FROM mongo c where "
                                        + "c.ad='555rs5' and "
                                        + "c.cId='44444sf' and "
                                        + "c.langId='59ecc8' and c.date < '"
                                        + tempDate + "' ORDER BY -date");
        query1.setFirstResult(firstResult);                 
        query1.setMaxResults(maxResult);
        List<mongo> = query1.list();
        //terminate loop if list is empty or records less than pagesize.
        if(list.isEmpty() || list.size() < (maxResult-firstResult)) {
            flag = false;   
        } else {
            firstResult = firstResult + pageSize;
            maxResult = maxResult + pageSize;
        }   
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM