Search in sources :

Example 1 with IteratorAdapter

use of com.mysema.commons.lang.IteratorAdapter in project querydsl by querydsl.

the class EclipseLinkHandler method iterate.

@SuppressWarnings("unchecked")
@Override
public <T> CloseableIterator<T> iterate(Query query, FactoryExpression<?> projection) {
    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();
        closeable = new Closeable() {

            @Override
            public void close() throws IOException {
                cursor.close();
            }
        };
        iterator = cursor;
    } else {
        iterator = query.getResultList().iterator();
    }
    if (projection != null) {
        return new TransformingIterator<T>(iterator, closeable, projection);
    } else {
        return new IteratorAdapter<T>(iterator, closeable);
    }
}
Also used : IteratorAdapter(com.mysema.commons.lang.IteratorAdapter) Closeable(java.io.Closeable) IOException(java.io.IOException) Cursor(org.eclipse.persistence.queries.Cursor) JpaQuery(org.eclipse.persistence.jpa.JpaQuery)

Example 2 with IteratorAdapter

use of com.mysema.commons.lang.IteratorAdapter in project querydsl by querydsl.

the class HibernateHandler method iterate.

@SuppressWarnings("unchecked")
@Override
public <T> CloseableIterator<T> iterate(Query query, FactoryExpression<?> projection) {
    if (query instanceof HibernateQuery) {
        HibernateQuery hQuery = (HibernateQuery) query;
        ScrollableResults results = hQuery.getHibernateQuery().scroll(ScrollMode.FORWARD_ONLY);
        CloseableIterator<T> iterator = new ScrollableResultsIterator<T>(results);
        if (projection != null) {
            iterator = new TransformingIterator<T>(iterator, projection);
        }
        return iterator;
    } else {
        Iterator<T> iterator = query.getResultList().iterator();
        if (projection != null) {
            return new TransformingIterator<T>(iterator, projection);
        } else {
            return new IteratorAdapter<T>(iterator);
        }
    }
}
Also used : IteratorAdapter(com.mysema.commons.lang.IteratorAdapter) HibernateQuery(org.hibernate.jpa.HibernateQuery) ScrollableResults(org.hibernate.ScrollableResults)

Aggregations

IteratorAdapter (com.mysema.commons.lang.IteratorAdapter)2 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)1 Cursor (org.eclipse.persistence.queries.Cursor)1 ScrollableResults (org.hibernate.ScrollableResults)1 HibernateQuery (org.hibernate.jpa.HibernateQuery)1