Suppose I have a query object like this:
final SQLQuery query = createQuery(); // org.hibernate.SQLQuery
// HibernateTransaction is just a wrapper around Hibernate's Transaction, Session and
// SessionFactory classes
// the corresponding method:
private SQLQuery createQuery(HibernateTransaction t) {
final SQLQuery query = t.fullSQLQuery(MY_QUERY_STRING);
query.addScalar("column1", Hibernate.LONG);
query.addScalar("column2", Hibernate.LONG);
query.setResultTransformer(new AliasToBeanResultTransformer(MyDTO.class));
return query;
}
The SQLQuery
object has a method called iterate()
. My problem is that If I try to do something like this:
Iterator<MyDTO> iterator = query.iterate();
while(iterator.hasNext()) {
MyDTO dto = iterator.next();
// ...
}
I get an exception that iteration is not supported. If I use the query.list()
method then I get a List<MyDTO>
which is nice but If my query returns a million rows it gets slow and eventually runs out of memory.
My question is that what is the idiomatic way using Hibernate to only fetch a row at a time and lazily iterate over them? In my example I try to process the data in a table row by row.
I was thinking about using clustered indexes and only query like 1000 rows once and the next 1000 will be based on the last id of the previous 1000 (so basically I was thinking about pagination) but we are using MyISAM tables which are not supporting this .