Skip to content

Projections should be interoperable with JPA EntityGraph #2519

Closed as not planned

Description

@bgiddens

JPA's EntityGraph feature resolves limitations of statically-defined eager and lazy fetch strategies. For more intro on that, Baeldung has a demo here.

SDR's projections are powerful, but run into exactly these limitations without a clear workaround. For example, one may want an endpoint to simultaneously support a shallow projection with fields from a single underlying table and a deep projection with many associations. If the best available solution is to statically define the fetch strategy on the entity, then one of those two projections is stuck with suboptimal performance - perhaps critically so. If associations are loaded eagerly, then the API does more work than needed for the shallow projection. If associations are loaded lazily, then the API makes a poor plan for populating the deeply nested fields. Dynamically selecting an entity graph based on the requested projection would resolve this.

For many typical use cases, defining entity graphs is very parallel to defining projections. They may even be one-to-one. Unfortunately, applying them in a repository context typically requires an annotation on the repository method, which breaks SDR's projection pattern by creating entirely separate endpoints.

I don't yet see an obvious solution for how to dynamically provide an entity graph at runtime when invoking repository methods by reflection, as SDR does. A potential future solution from outside the SDR scope could be an enhancement to JPA's @EntityGraph annotation to support a component that provides the entity graph at runtime, instead of by a statically defined string at compile time.

Nonetheless, I want to open this issue in case a solution to this problem has been discussed before, and to get the ball rolling on what might be a valuable feature enhancement in a future version. Perhaps some of the more experienced contributors in this repo will see an answer that I've missed.

Activity

  1. odrotbohm commented on Jan 15, 2026

    @odrotbohm
    Member

    The fundamental misassumption here is that EntityGraphs are a good idea in the first place, but they – by design – aren't. They overload the primary entity type (the aggregate) with the responsibility to create partial representations. This usually causes the original aggregates to accumulate more and more properties and relationships as some arbitrary projection (not in the Spring Data sense, but generally) needs that property.

    The obvious solution – and we promote that through all of Spring Data – is to separate the concerns. The aggregate holds the state that's needed to implement business-driven state transitions in a strongly consistent way. Any kind of views onto them that might be needed in related operations need to be driven by separate models. Spring Data supports that through interface-based or DTO projections.

    Spring Data REST doesn't (and must not!) care about persistence technology-specific mechanisms in data retrieval. It's solely built on the notion of aggregates and repositories.

  2. added
    status: declinedA suggestion or change that we don't feel we should currently apply
    and removed on Jan 15, 2026
  3. bgiddens commented on Apr 28, 2026

    @bgiddens
    Author

    Thanks for the response! Since my first post, I dug into the underlying JPA implementation for EntityGraphs and I agree with you. Sharing my workaround here for future readers.

    To your point, none of the interfaces in org.springframework.data.repository support a way for higher-level processes like Spring Data REST to influence the data retrieval strategy, such as via an entity graph. You hit the nail on the head with this:

    Spring Data REST doesn't (and must not!) care about persistence technology-specific mechanisms in data retrieval.

    So as far as this issue and project are concerned, that's case closed.

    Still, SDR's support for various projections via a single endpoint and underlying repository method introduces a potential demand to be able to adjust the load strategy for a single repository operation at runtime (i.e., it might be better to fetch an association eagerly if it is included in a projection but lazily if not). So an adapter between the two systems would be valuable.

    I put together an automagical workaround for this here, although I would hedge that this abuses the limits of JPA's API. It's a proof-of-concept.

    Related reading for anyone interested: this repository extends JPA repository patterns to support parametric control of entity graphs. Very cool and probably a better foundation for the workaround I built, although these would need a custom invoker to work with Spring Data REST. And of course, all of this is specific to JPA implementations since entity graphs are not part of the general Spring repository concept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

status: declinedA suggestion or change that we don't feel we should currently apply

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions