开发者

Avoid having a huge collection of ids by calling a DAO.getAll()

开发者 https://www.devze.com 2022-12-26 17:19 出处:网络
Instead of returning a List<Long> of ids when calling PersonDao.getAll() we wanted not to have an entire collection of ids in memory.

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.springfram开发者_JS百科ework.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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消