Skip to content
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

EntityGraph fetches same relation multiple times [DATAJPA-1123] #1468

Closed
spring-projects-issues opened this issue May 25, 2017 · 3 comments
Closed
Assignees
Labels
in: core Issues in core support status: declined A suggestion or change that we don't feel we should currently apply type: bug A general bug

Comments

@spring-projects-issues
Copy link

Yan Tuizhi opened DATAJPA-1123 and commented

Assuming I have the following entites

House (1)<->(n) Room (1)<->(n) Door

which I want to fetch via a @EntityGraph annotation on a repository method. All assocations are set to FetchType.LAZY. The repository method is defined the following way:

@EntityGraph(attributePaths = {"rooms", "rooms.doors"})
@Query("FROM House")
List<House> findAllWithRoomsAndDoors();

If there is one house with one room and three doors, then this repository method will return

  • three instances of the same house
  • each house has three times the same room
  • each room has all three doors

This is what I would expect as result of a SQL query, when joining over all three tables. But is this also the intended behaviour of a repository? I think it is not. The ORM should handle the relations correctly. And indeed, when setting all associations to FetchType.EAGER and calling the repository method findAll(), only one house is fetched with one room and three doors. Of course I could change List<House> to Set<House> as return type of the repository method defined above, but the repository nevertheless tries to add the same house three times. Furthermore, I need lists for some operations.

I see three possible solutions:

  • Either keeping one join over all tables and handling the adding of associations correctly.
  • For each given attribute path, exactly one query is exectued (First get the Houses, then Rooms WHERE house IN ([found primary keys of houses]), then Doors WHERE room IN ([found primary keys of room])
  • Or allowing the definition of subgraphs which will be fetched by a join, i.e. the definition of partitions of the whole graph.

The last option subsumes both the first and the second one (the first and second option are special cases of the third one) and would be in my opinion the best solution. Sometimes it is much more performant to partition the fetching of a complex graph, especially if the graph is complex and there are many relations.


Affects: 1.11.3 (Ingalls SR3)

@spring-projects-issues
Copy link
Author

Yan Tuizhi commented

I now rather think it is an Hibernate issue. Am I right? Should I file an issue in the Hibernate bug tracker?

@spring-projects-issues
Copy link
Author

Jens Schauder commented

Yes this sounds like an issue of the JPA implementation.

When opening an issue with Hibernate, make sure NOT to include any Spring dependency. They don't like that :-)

Please leave a link to any issue you create.

@spring-projects-issues
Copy link
Author

Jens Schauder commented

This is not a Spring Data issue

@spring-projects-issues spring-projects-issues added type: bug A general bug status: declined A suggestion or change that we don't feel we should currently apply in: core Issues in core support labels Dec 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core support status: declined A suggestion or change that we don't feel we should currently apply type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants