Java 设置hibernate中的最大结果不符合要求
这是我使用HQL编写的查询Java 设置hibernate中的最大结果不符合要求,java,hibernate,jakarta-ee,Java,Hibernate,Jakarta Ee,这是我使用HQL编写的查询。我的要求如下。 我有45张唱片,我想每次拿10张。它成功地提供了多达40条记录。但从41-45条记录中,查询返回空列表 query1 = session .createQuery( "FROM mongo c where " + "c.ad='555rs5' and "
。我的要求如下。 我有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);
query1=会话
.createQuery(
“来自mongo c where”
+“c.ad='555rs5'和”
+“c.cId='44444平方英尺'和”
+“c.langId='59ecc8'和c.date<'”
+tempDate+“‘订单截止日期”)
.setMaxResults(10);
我的问题有错吗?请让我知道问候
Naresh Veluri对于Hibernate分页,在
setMaxResults()
方法旁边,还需要setFirstResult()
方法。查看我想你的循环中一定有问题
根据K.C.的回答,您没有设置setFirstResult(),我不知道您的查询如何获取下一条记录。请阅读
尝试下面的代码,希望这将帮助您解决您的问题
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;
}
}
boolean标志=true;
int firstResult=0;
int pageSize=10;
int maxResult=10;
while(旗帜){
query1=session.createQuery(“来自mongo c where”
+“c.ad='555rs5'和”
+“c.cId='44444平方英尺'和”
+“c.langId='59ecc8'和c.date<'”
+tempDate+“‘订单截止日期’”;
查询1.setFirstResult(firstResult);
查询1.setMaxResult(maxResult);
List=query1.List();
//如果列表为空或记录小于pagesize,则终止循环。
if(list.isEmpty()| | list.size()<(maxResult firstResult)){
flag=false;
}否则{
firstResult=firstResult+pageSize;
maxResult=maxResult+pageSize;
}
}
每次提取10条记录是什么意思?你能给我们看完整的代码吗?我的数据库里有45条记录。我想通过调用数据库服务,每次获取10条记录。
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;
}
}