I got this repository code:
@Query(value = "select distinct r from Reference r " +
"inner join fetch r.persons " +
"left outer join fetch r.categories " +
"left outer join fetch r.keywords " +
"left outer join fetch r.parentReferences",
countQuery = "select count(distinct r.id) from Reference r " +
"inner join r.persons " +
"left outer join r.categories " +
"left outer join r.keywords " +
"left outer join r.parentReferences")
Page<Reference> findsAllRelevantEntries(Pageable pageable);
When i run tests on that query i got this Hibernate warning:
HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!
@Test
void testFindAllRelevantAsync() throws ExecutionException, InterruptedException {
CompletableFuture<Page<Reference>> all = referenceService.findAllRelevantAsync(PageRequest.of(1, 20));
CompletableFuture.allOf(all).join();
assertThat(all.get()).isNotNull();
assertThat(all.get()).isNotEmpty();
}
The repository code is encapsulated in a service method not shown here. It (the service method) just marshalls the call from the service to the repository and back.
Furthermore the generated sql query does not generate a limit
clause. Though it does fire two queries.
One for the count
, the other for the fetching of all records.
So it fetches all records and applies the pagination in memory.
This leads to a very slow query execution.
How can i make pagination work with this query?
EDIT
I know that this here is often suggested as solution: How can I avoid the Warning "firstResult/maxResults specified with collection fetch; applying in memory!" when using Hibernate?
Is there a way to achieve the pagination with Spring Data JPA?
I don´t want to hardwire either an EntityManager
, neither i want to
extend code from a BasicTransformerAdapter