I have QueryDsl query with Spring jpa written like:
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 is used because data from joined tables is needed. The strange thing is that some of these queries have correct SQL ending on limit ?,?
. It means pagination is applied inside the database.
But if the mapping is done via @OneToMany(mappedBy = "...")
and not @JoinColumn()
, then all data is loaded into memory and I see the warning:
HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!
How to avoid loading all data into memory in this case?