Hibernate:Criteria 忽略 fetchSize 参数并获取比请求更多的行
Hibernate : Criteria ignoring fetchSize parameter and fetching more rows than asked
我正在开发一个 Spring-MVC 应用程序,其中我有一个使用 Criteria 编写的搜索功能。对于分页搜索,我从前端获取 firstResult 和 fetchSize 作为参数。不幸的是,Criteria 忽略了它们并在仅询问 20 行时返回了一个巨大的列表(大约 1000 行)。出了什么问题?
代码:
System.out.println("Fetch size is "+fetchSize);
System.out.println("First result is "+firstResult);
Criteria andCriteria = session.createCriteria(Host.class);
Conjunction and = Restrictions.conjunction();
if((studentSearchHistory.getCity()!=null) && (!(studentSearchHistory.getCity().isEmpty()))) {
and.add(Restrictions.ilike("city", studentSearchHistory.getCity()));
}
//Other search conditions
andCriteria.setFetchSize(fetchSize);
andCriteria.setFirstResult(firstResult);
andCriteria.add(and);
hostList.addAll(andCriteria.list());
if(hostList != null){
System.out.println("Host list size is "+hostList.size());
}
输出:
Fetch size is 10
First result is 0
Host list size is 1003
我做错了什么?谢谢你。
也许你应该在这里使用 setMaxResults
。
Here是这两种方法的比较。
fetchSize
与查询导出的记录数无关。与获取的记录数有关。
您需要使用setMaxResults
来限制休眠检索的记录数。
Set the maximum number of rows to retrieve. If not set, there is no limit to the number of rows retrieved.
setFetchSize
仅与休眠内部如何获取记录组(是一种缓存)有关。
Set a fetch size for the underlying JDBC query.
并且根据 Statement 的描述:
Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for ResultSet objects genrated by this Statement. If the value specified is zero, then the hint is ignored. The default value is zero.
我正在开发一个 Spring-MVC 应用程序,其中我有一个使用 Criteria 编写的搜索功能。对于分页搜索,我从前端获取 firstResult 和 fetchSize 作为参数。不幸的是,Criteria 忽略了它们并在仅询问 20 行时返回了一个巨大的列表(大约 1000 行)。出了什么问题?
代码:
System.out.println("Fetch size is "+fetchSize);
System.out.println("First result is "+firstResult);
Criteria andCriteria = session.createCriteria(Host.class);
Conjunction and = Restrictions.conjunction();
if((studentSearchHistory.getCity()!=null) && (!(studentSearchHistory.getCity().isEmpty()))) {
and.add(Restrictions.ilike("city", studentSearchHistory.getCity()));
}
//Other search conditions
andCriteria.setFetchSize(fetchSize);
andCriteria.setFirstResult(firstResult);
andCriteria.add(and);
hostList.addAll(andCriteria.list());
if(hostList != null){
System.out.println("Host list size is "+hostList.size());
}
输出:
Fetch size is 10
First result is 0
Host list size is 1003
我做错了什么?谢谢你。
也许你应该在这里使用 setMaxResults
。
Here是这两种方法的比较。
fetchSize
与查询导出的记录数无关。与获取的记录数有关。
您需要使用setMaxResults
来限制休眠检索的记录数。
Set the maximum number of rows to retrieve. If not set, there is no limit to the number of rows retrieved.
setFetchSize
仅与休眠内部如何获取记录组(是一种缓存)有关。
Set a fetch size for the underlying JDBC query.
并且根据 Statement 的描述:
Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for ResultSet objects genrated by this Statement. If the value specified is zero, then the hint is ignored. The default value is zero.