I have been prototyping my new application in Spring Data REST backed by Spring Data JPA & Hibernate, which has been a fantastic productivity booster for my team, but as the data model becomes more complex the performance is going down the tubes. Looking at the executed SQL, I see two separate but related problems:
When using a
Projection
with only a few properties to reduce the size of my payload, SDR is still loading the entire entity graph, with all the overhead that incurs. EDIT: filed DATAREST-1089There seems to be no way to specify eager loading using JPA, as SDR auto-generates the repository methods so I can't add
@EntityGraph
to them. (and per DATAREST-905 below, even that doesn't work) EDIT: addressed in Cepr0's answer below, though this can only be applied to each finder method once. See DATAJPA-749
I have one key model that I use several different projections of depending on the context (list page, view page, autocomplete, related item page, etc), so implementing one custom ResourceProcessor
doesn't seem like a solution.)
Has anyone found a way around these problems? Otherwise anyone with a non-trivial object graph will see performance deteriorate drastically as their model grows.
My research:
- How do I avoid n+1 queries with Spring Data Rest? (from 2013)
- https://jira.spring.io/browse/DATAJPA-466 ( Add support for lazy loading configuration via JPA 2.1 fetch-/loadgraph.)
- https://jira.spring.io/browse/DATAREST-905 ( No way to avoid loading all child relations in spring-data-rest?) (2016, unanswered)