Skip to content

Projections should be interoperable with JPA EntityGraph #2519

@bgiddens

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions