0

I am using a DAO framework named "QueryDSL". When I launch a query, QueryDSL fetches all results together that isnot desired. I am looking for a soultion that lets me to load the results in lazy form.

3 Answers 3

1

I think you need kind of pagination there.

Read here how to organize it. You need something like this to load part of your data

Page<T> findAll(com.mysema.query.types.Predicate predicate,
                Pageable pageable)
Sign up to request clarification or add additional context in comments.

Comments

0

I eventually implemented a lazy list. This works well. :)

public class LazyList<T> implements List<T> {

private SQLQuery query;
private long size = 0;
private int limit = 100;

private Expression<T> exprsn;
private int offset = 0;
private List<T> cache = new ArrayList<>();

public LazyList(SQLQuery query, Expression<T> exprsn) {
    this.query = query;
    this.exprsn = exprsn;
    size = query.count();
    cache = query.limit(limit).offset(offset).list(exprsn);
}

@Override
public int size() {
    return (int) size;
}

@Override
public boolean isEmpty() {
    return size == 0;
}

@Override
public T get(int index) {
    if (index < offset || index > offset + limit) {
        cache = query.limit(limit).offset(offset).list(exprsn);
    }
    return cache.get(index - offset);
}

@Override
public Iterator<T> iterator() {
    return new Iterator<T>() {

        private int index = 0;
        private int offset = 0;
        private List<T> cache = new ArrayList<>();

        {
            cache = query.limit(limit).offset(offset).list(exprsn);
        }

        @Override
        public boolean hasNext() {
            return index < size;
        }

        @Override
        public T next() {
            if (index < offset || index > offset + limit) {
                cache = query.limit(limit).offset(offset).list(exprsn);
            }
            return cache.get(index - offset);
        }
    };
}

@Override
public boolean contains(Object o) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Object[] toArray() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public <T> T[] toArray(T[] a) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean add(T e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean remove(Object o) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean containsAll(Collection<?> c) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean addAll(Collection<? extends T> c) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean addAll(int index, Collection<? extends T> c) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean removeAll(Collection<?> c) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean retainAll(Collection<?> c) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void clear() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public T set(int index, T element) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void add(int index, T element) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public T remove(int index) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public int indexOf(Object o) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public int lastIndexOf(Object o) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public ListIterator<T> listIterator() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public ListIterator<T> listIterator(int index) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public List<T> subList(int fromIndex, int toIndex) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

4 Comments

What if size overflows?
This is a too simple implementation that just presents idea and needs to be completed for a real production. However, this satisfies my simple requirements. :)
Okay, but you shouldn't do such casts. Why do you need it long if you're only going to work with the 32 bits...
Because query.count return a long value whereas list interface needs a integer. So, casting is a straightforward way. What is ur solution?
0

Querydsl SQL doesn't support lazy loading, in Querydsl JPA lazy loading is handled by the JPA provider.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.