Implicit join on one-to-many relations
I really like the simplicity of spring-data-jdbc, but the default behaviour somehow breaks my understanding of efficient loading.
Problem description Having one-to-many relations in an aggregate root always causes N+1 queries. There is no way to change this so if I have the following structure, I end up with many redundant SQL statements.
Minion
class Minion {
@Id
Long id;
String firstName;
String lastName;
@MappedCollection(idColumn = "minion_id", keyColumn = "id")
final Set<Toy> toys = new HashSet<>();
}
Toy
class Toy {
@Id
Long id;
String name;
Long minionId;
}
Expected behaviour Change the default behaviour and load all the relations with implicit join. I assume that the library has the information
Environment org.springframework.boot:spring-boot-starter:2.5.6 org.springframework.boot:spring-boot-starter-data-jdbc: 2.5.6 org.springframework.data:spring-data-jdbc:2.2.6
We are working on a more efficient way to load aggregates.
@schauder Is there another ticket related to this activity?
I ended up with the same realisation after turning on sql trace.
Meanwhile what could be done, is using a projection with a custom query.
This help prevent the loading of one-to-many relationship, which help make the system more responsive
@petromir Found this ticket which was created in 2019 https://github.com/spring-projects/spring-data-relational/issues/592
duplicate of #592