views:

75

answers:

1

Instead of returning a List<Long> of ids when calling PersonDao.getAll() we wanted not to have an entire collection of ids in memory.

Seems like returning a org.springframework.jdbc.support.rowset.SqlRowSet and iterate over this rowset would not hold every object in memory.

The only problem here is i cannot cast this row to my entity.

Is there a better way for this?

Generally we want to do a method on every Person in our db

+1  A: 

You could use ScrollableResults to iterate through the result set, and clear the session regularly to dispose of unneeded objects. Example from the Hibernate book:

ScrollableResults itemCursor = session.createQuery("from Item").scroll();
int count=0;

while ( itemCursor.next() ) {
  Item item = (Item) itemCursor.get(0);
  modifyItem(item);
  if ( ++count % 100 == 0 ) {
    session.flush();
    session.clear();
  }
}

See the Hibernate reference for more examples and details.

Péter Török
great answer! except flushing and clearing i'm afraid of. Do i want this also for other running transactions... StatelessSession looks best for me.
Michael Bavin
@Michael Good point, I forgot to mention that this is supposed to be executed in a separate transaction, not to mess up the persistence context. But a StatelessSession can also work.
Péter Török
@Péter: Ok but when this is running in a sperate transaction, a session.flush/clear will still affect all transactions in this session, i think.
Michael Bavin
@Michael Oops, my bad. I meant **session** indeed, not transaction :-(
Péter Török