0

I am attempting to use Pageable and org.springframework.data.domain.Page to get a list of entities. We only want one page at a time to be retrieved.

The problem arises when I attempt to access the returned list. When it is within a transaction, when you access the 2nd entity (yes, not the first), it seems to attempt to fetch everything.

If I access the list outside of the transaction, it does not do this. But since we are lazily loading properties, when we attempt to access the lazily loaded properties it throws an error because it is outside of the transaction.

How can I achieve only fetching the specific page we request and being able to access the lazy loaded properties?

Thanks in advance for the help.

Lima Jim
  • 11
  • 1
  • Could you elaborate on 'seems to attempt to fetch everything'? Are new entities being added to the result that extends beyond a single page? What are the symptoms of 'fetching everything' that you observed? – crizzis Nov 07 '20 at 19:19
  • It is extending beyond a single page. It looks like the cause of this is that we are using entity graphs. – Lima Jim Nov 09 '20 at 12:23
  • Well, if you're fetching related to-many associations, Spring Data cannot limit query results at the query level, it has to calculate the pages in memory. That's because, when you join a multivalued association, the number of sql result rows corresponding to a single JPQL result becomes variable – crizzis Nov 09 '20 at 18:04

1 Answers1

1

This seems to be a hibernate issue with using entity graphs: https://github.com/Cosium/spring-data-jpa-entity-graph/issues/29

Lima Jim
  • 11
  • 1