Reputation: 13
I am using the following piece of code to get results from DB as result set is huge.
Is it the correct way doing? I am running in to Out of memory exception and I suspect it is because of this.
// 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
}
}
Note:
jboss run.conf
.createQuery
as in that case hibernate is generating lots of queries, so i am using a big join query, along with createSQLQuery
.Upvotes: 1
Views: 1563
Reputation: 242706
Your code looks strange - the correct way to paginate results would be this:
bigQ.setFirstResult(i * div);
bigQ.setMaxResults(div);
Also make sure that your session cache doesn't grow - if your query returns only scalar values (not entities) it shouldn't be a problem, but if you load some entities in // rest of operations
, it can.
Upvotes: 2
Reputation: 5848
Use offset and limit... http://www.petefreitag.com/item/451.cfm
Upvotes: 0