Skip to content

Implicit join on one-to-many relations #1330

Not planned
@petromir

Description

@petromir

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

Activity

schauder

schauder commented on Sep 19, 2022

@schauder
Contributor

We are working on a more efficient way to load aggregates.

petromir

petromir commented on Sep 27, 2022

@petromir
Author

@schauder Is there another ticket related to this activity?

benzen

benzen commented on Nov 25, 2022

@benzen

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

benzen

benzen commented on Nov 29, 2022

@benzen

@petromir Found this ticket which was created in 2019
#592

schauder

schauder commented on Nov 30, 2022

@schauder
Contributor

duplicate of #592

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Implicit join on one-to-many relations · Issue #1330 · spring-projects/spring-data-relational