开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜