我使用以下代码来获取DB的结果,因为结果集很大。
这是正确的做法吗? 我正在运行内存不足异常,我怀疑是因为这个。
// div is set to 10,000
// i have calculated how many times i need do it using a count query
// and that value is being used in iteration variable
Query bigQ=session.createSQLQuery(bigQuery);
for(int i=0;i<iteration;i++)
{
bigQ.setFetchSize(div);
bigQ.setMaxResults(i*div);
List<Object[]> result=bigQ.list();
// now i am using the result to get the values
for(Object[] a:result)
{
// rest of operations
}
}
注意:
jboss run.conf
。createQuery
,因为在这种情况下,hibernate会生成大量查询,因此我使用了大型连接查询以及createSQLQuery
。答案 0 :(得分:2)
您的代码看起来很奇怪 - 分页结果的正确方法是:
bigQ.setFirstResult(i * div);
bigQ.setMaxResults(div);
还要确保会话缓存不会增长 - 如果您的查询仅返回标量值(而不是实体),那么它应该不是问题,但如果您在// rest of operations
中加载某些实体,它就可以。
答案 1 :(得分:0)
使用偏移和限制... http://www.petefreitag.com/item/451.cfm