Skip to content

EmbeddedResourcesAssembler fetches lazy-loaded associations unnecessarily #2520

Closed as not planned

Description

@bgiddens

Consider an entity with a lazy-loaded one-to-many association, and a simple shallow projection that excludes the association. When retrieving the entity collection with the shallow projection, we would hope and expect that the association is never loaded.

In practice, SDR forces loading of the association via the EmbeddedResourcesAssembler's getEmbeddedResources method. This can become a significant performance problem in complex entity networks with many associations and large-scale one-to-many relationships.

I am not 100% clear on the scope of responsibility of the the EmbeddedResourcesAssembler (does it assemble association links, nested response models, or both?), but it is clearly aware of the projection context via its projector member. It can use this to check if the actual value of the association is needed in the projection. If not, then the property fetch done here and the subsequent collection processing can be skipped; all that should be needed is a link to the relevant relation endpoint.

Activity

  1. odrotbohm commented on Jan 15, 2026

    @odrotbohm
    Member

    Consider an entity with a lazy-loaded one-to-many association…

    That's a mismodeled aggregate in the first place. If the target of the relationship is a non-aggregate, that relationship should be materialized eagerly. If the target is an aggregate in the first place, the relationship should be rather referring to identifiers, not full objects.

    In practice, SDR forces loading of the association via the EmbeddedResourcesAssembler's getEmbeddedResources method.

    Force is a strong word here, but we somehow need to render the embedded resources when creating the representation.

    This can become a significant performance problem in complex entity networks with many associations and large-scale one-to-many relationships.

    Especially the latter are an anti-pattern in aggregate design. Remember that Spring Data REST is not an arbitrary “expose your database via JPA and HTTP” tool. It's heavily relying on aggregates being modeled as they, in contrast to relational databases, naturally form trees. That's necessary, as only then we can reason about which relationships are included in the primary representation and which ones to represent as links to other resources.

    I.e., you're trying to fix the messenger when the source of the problem is the aggregate design. Large, likely unbounded one-to-many relationships can usually be replaced with an inverted relationship and a findBy$Owner(…) query method on the repository.

  2. spring-projects-issues commented on Jan 22, 2026

    @spring-projects-issues

    If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

  3. spring-projects-issues commented on Jan 29, 2026

    @spring-projects-issues

    Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions