首页 > 解决方案 > 带分页的QueryDsl

问题描述

我有 Spring jpa 的 QueryDsl 查询,如下所示:

JPAQuery<FooEntity> query = getQuerydsl()
        .<FooEntity>createQuery()
        .from(foo)
        .leftJoin(foo.bar, bar).fetchJoin()
        .leftJoin(...).fetchJoin()
        .where(predicate)
        .distinct();
    return new PageImpl<>(getQuerydsl().applyPagination(pageable, query).fetch(), pageable,
        query.fetchCount());

使用 FetchJoin 是因为需要来自连接表的数据。奇怪的是,其中一些查询具有正确的 SQL 结尾limit ?,?。这意味着在数据库内部应用了分页。

但如果映射是通过@OneToMany(mappedBy = "...")and not完成的@JoinColumn(),那么所有数据都会加载到内存中,我会看到警告:

HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!

在这种情况下如何避免将所有数据加载到内存中?

标签: javaspringjpaquerydsl

解决方案


推荐阅读