Skip to content

EntityGraphFactory overwrites nested subgraphs #2527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pstrnad opened this issue May 10, 2022 · 1 comment
Closed

EntityGraphFactory overwrites nested subgraphs #2527

pstrnad opened this issue May 10, 2022 · 1 comment
Assignees
Labels
in: repository Repositories abstraction type: bug A general bug

Comments

@pstrnad
Copy link
Contributor

pstrnad commented May 10, 2022

I'm trying to eager join OneToMany mappings of three entities, e.g.:

class A {
    int id;

    @OneToMany
    Set<B> b;
}
class B {
    int id;

    @OneToMany
    Set<C> c;
}
class C {
    int id;
}

When fetching an object of class A, the EntityGraphFactory.create method receives in the "properties" parameter a set of:
"b.c.id",
"b.id",
"id"
As it processes the "b.c.id" property, it adds a subgraph for B to the entityGraph, then adds a subgraph for C to the subgraph for B.
However, when it proceeds to the "b.id" property, it adds a new subgraph for B to the entityGraph and overrides the previous one. Instead, it should check if the entityGraph already contains a subgraph and enrich it.

I ran into this when integrating Spring Data JPA with Spring GraphQL, and noticed that a GraphQL query

{
    a {
        b {
            c {
                id
            }
        }
    }
}

correctly generates a single SQL query with joins, but a query

{
    a {
        b {
            id,
            c {
                id
            }
        }
    }
}

does not, and instead triggers N+1 lazy loads

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 10, 2022
@gregturn gregturn self-assigned this May 16, 2022
pstrnad added a commit to pstrnad/spring-data-jpa that referenced this issue Jun 18, 2022
pstrnad added a commit to pstrnad/spring-data-jpa that referenced this issue Jun 18, 2022
pstrnad added a commit to pstrnad/spring-data-jpa that referenced this issue Jun 18, 2022
pstrnad added a commit to pstrnad/spring-data-jpa that referenced this issue Jun 20, 2022
@gregturn gregturn added this to the 3.1 M1 (2022.1.0) milestone Dec 21, 2022
@gregturn gregturn added type: bug A general bug in: repository Repositories abstraction and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 21, 2022
@gregturn
Copy link
Contributor

gregturn commented Dec 21, 2022

Backported to 3.0.x and 2.7.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: repository Repositories abstraction type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants