Let\'s say I have a table with millions of rows. Using JPA, what\'s the proper way to iterate over a query against that table, such that I don\'t have all an in-memo
If you use EclipseLink I' using this method to get result as Iterable
private static Iterable getResult(TypedQuery query)
{
//eclipseLink
if(query instanceof JpaQuery) {
JpaQuery jQuery = (JpaQuery) query;
jQuery.setHint(QueryHints.RESULT_SET_TYPE, ResultSetType.ForwardOnly)
.setHint(QueryHints.SCROLLABLE_CURSOR, true);
final Cursor cursor = jQuery.getResultCursor();
return new Iterable()
{
@SuppressWarnings("unchecked")
@Override
public Iterator iterator()
{
return cursor;
}
};
}
return query.getResultList();
}
close Method
static void closeCursor(Iterable> list)
{
if (list.iterator() instanceof Cursor)
{
((Cursor) list.iterator()).close();
}
}