在QueryDSL中进行延迟加载

时间:2015-07-21 07:09:58

标签: java dao querydsl

我正在使用名为“QueryDSL”的DAO框架。当我启动查询时,QueryDSL将所有结果一起取出,这是不需要的。我正在寻找一种灵魂,让我以懒惰的形式加载结果。

3 个答案:

答案 0 :(得分:1)

我认为你需要那种分页。

阅读here如何整理它。你需要这样的东西来加载部分数据

Page<T> findAll(com.mysema.query.types.Predicate predicate,
                Pageable pageable)

答案 1 :(得分:0)

我最终实现了一个懒惰的列表。这很好用。 :)

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.
}

}

答案 2 :(得分:0)

Querydsl SQL不支持延迟加载,在Querydsl中,JPA延迟加载由JPA提供程序处理。