繁体   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