Skip to content

@EntityGraph(attributePaths) is ignored if there is a "fetch" in @Query #3140

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
gavenkoa opened this issue Sep 4, 2023 · 1 comment
Closed
Labels
for: external-project For an external project and not something we can fix

Comments

@gavenkoa
Copy link

gavenkoa commented Sep 4, 2023

We noticed that @EntityGraph(attributePaths = ...) is ignored when there is fetch in @Query.

For:

    @EntityGraph(attributePaths = {"upgradeRequest.segment"})
    @Query("select j from UpgradeJob j" +
            " join fetch j.upgradeRequest r" +
//            " join fetch j.upgradeRequest.segment" +
            " where j.status = :status and r.pnrId = :pnrId")
    List<UpgradeJob> findByStatusAndPnrId(UpgradeJobStatus status, Long pnrId);

we get - notice - no join to segment:

    from
        upgrade_job u1_0 
    join
        upgrade_request u2_0 
            on u2_0.id=u1_0.request_id 
    where
        u1_0.status=? 
        and u2_0.pnr_id=?

We changed the query to explicit fetch:

    @Query("select j from UpgradeJob j" +
            " join fetch j.upgradeRequest r" +
            " join fetch j.upgradeRequest.segment" +
            " where j.status = :status and r.pnrId = :pnrId")
    List<UpgradeJob> findByStatusAndPnrId(UpgradeJobStatus status, Long pnrId);

so we have join to segment, avoiding N+1:

    from
        upgrade_job u1_0 
    join
        upgrade_request u2_0 
            on u2_0.id=u1_0.request_id 
    join
        pnr_segment s1_0 
            on s1_0.id=u2_0.segment_id 
    where
        u1_0.status=? 
        and u2_0.pnr_id=?

Could we have at least warning, telling that @EntityGraph will be ignored?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 4, 2023
@mp911de
Copy link
Member

mp911de commented Sep 5, 2023

That's rather a Hibernate issue as Spring Data generally applies the EntityGraph if it is specified.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Sep 5, 2023
@mp911de mp911de added for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

3 participants