Open alperkrtglu opened 3 months ago
The current project passes the EntityGraph to the ORM. What the ORM does with it is the responsibility of the orm.
However, when we use JPA's EntityGraph, it queries with a single SQL and fetches the data. In this case, I did not encounter the N + 1 problem.
@NamedEntityGraph(name = "Properties",
attributeNodes = [NamedAttributeNode("properties")])
@EntityGraph(value = "Properties")
override fun findById(id: UUID): Optional<AJpaEntity>
I believe this can also be done in your library. But what I am curious about here is whether we can use NamedEntityGraph and DynamicEntityGraph together? Do you have any recommendations?
Your second example uses named entity graph. Try with dynamic graph via the Spring Data @EntityGraph annotation. In any case, you must provide a reproduction case (code that just need to be cloned and runned).
What steps will reproduce the problem?
What is the expected output? A single query or a reduced number of queries that fetch AJpaEntity and the related BJpaEntity instances without causing an N + 1 problem, using the DynamicEntityGraph.
What happens instead? The actual behavior is that the system performs a join with the intermediary table and then issues separate queries to fetch each BJpaEntity instance related to AJpaEntity, resulting in an N + 1 query issue.
Environment
Additional context In the current setup, attempting to dynamically include a ManyToMany relationship in a single fetch operation using DynamicEntityGraph leads to inefficient query patterns. A solution or workaround that effectively reduces the number of generated queries while maintaining the flexibility of dynamic entity graphs is sought.