mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-30 21:08:30 +08:00
Merge pull request #2335 from rdicroce/cursor_fix
Fix bugs in EclipseLinkHandler#iterate()
This commit is contained in:
commit
becbbc0ee7
@ -17,10 +17,11 @@ import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.eclipse.persistence.config.HintValues;
|
||||
import org.eclipse.persistence.config.QueryHints;
|
||||
import org.eclipse.persistence.config.ResultSetType;
|
||||
import org.eclipse.persistence.jpa.JpaQuery;
|
||||
import org.eclipse.persistence.queries.Cursor;
|
||||
|
||||
@ -54,20 +55,40 @@ class EclipseLinkHandler implements QueryHandler {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> CloseableIterator<T> iterate(Query query, FactoryExpression<?> projection) {
|
||||
boolean canUseCursor = false;
|
||||
try {
|
||||
canUseCursor = query.unwrap(Query.class) instanceof JpaQuery;
|
||||
} catch (PersistenceException e) { } // can't unwrap, just ignore the exception
|
||||
|
||||
Iterator<T> iterator = null;
|
||||
Closeable closeable = null;
|
||||
if (query instanceof JpaQuery) {
|
||||
JpaQuery<T> elQuery = (JpaQuery<T>) query;
|
||||
elQuery.setHint(QueryHints.RESULT_SET_TYPE, ResultSetType.ForwardOnly);
|
||||
elQuery.setHint(QueryHints.SCROLLABLE_CURSOR, true);
|
||||
final Cursor cursor = elQuery.getResultCursor();
|
||||
if (canUseCursor) {
|
||||
query.setHint(QueryHints.CURSOR, HintValues.TRUE);
|
||||
final Cursor cursor = (Cursor) query.getSingleResult();
|
||||
final int pageSize = cursor.getPageSize();
|
||||
closeable = new Closeable() {
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
cursor.close();
|
||||
}
|
||||
};
|
||||
iterator = cursor;
|
||||
iterator = new Iterator<T>() {
|
||||
private int rowsSinceLastClear = 0;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return cursor.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
if (rowsSinceLastClear++ == pageSize) {
|
||||
rowsSinceLastClear = 0;
|
||||
cursor.clear();
|
||||
}
|
||||
return (T) cursor.next();
|
||||
}
|
||||
};
|
||||
} else {
|
||||
iterator = query.getResultList().iterator();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user