Spring Data JDBC - One to One & One to Many Relationships
Why Stack Overflow recommends it
Stack Overflow users link it as the reference example when asking or answering how Spring Data JDBC maps child entities and relationships within an aggregate.
Recommended by 1 people across 1 different months, 2020 to 2025.
What it is
A 10-minute tutorial by Maciej Walkowiak showing how to map one-to-one and one-to-many relationships in Spring Data JDBC, using a Movie aggregate root with Rental entities. It is based on the 1.1 snapshot, so some mapping details differ from release 1.0.2.
What people said
Recommends theNikki1 · 2025-04 · Stack Overflow · on “Spring data jdbc and child entity equality”
I’m studying Spring Data JDBC through Maciej Walkowiak’s video ( [this video] ), which presents a Movie aggregate root with a one-to-many relationship to Rental entities. The Rental entity lacks an @Id field, and no equals/hashCode is implemented. The rental table has an auto-incrementing id (not mapped to the entity) and an implicit movie_id foreign key. Here’s the simplified structure from the video: ]@Table("movie") class Movie { @Id Long id; String title; Set<Rental> rentals; } @Table("rental") class Rental { Duration duration; // No @Id, no equals/hashCode Integer price; } My Concern: The absence of equals/hashCode in Rental troubles me because, in DDD, entities should have identity-based eq
Cites Dias Abdraimov · 2020-05 · Stack Overflow · on “Spring Data JDBC org.springframework.data.mapping.MappingException: Could not read value rental_movie from result set”
How to select query filter in one-to-one relationship with Spring Data JDBC ? Schema looks like this, basically 2 tables where Rental references Movie drop table if exists rental; drop table if exists movie; create table movie ( id serial primary key, title text, description text ); create table rental ( movie integer primary key references movie (id), duration text, price integer ) And my code looks like this @Query("select * from movie where title = :title ") fun findByTitle(@Param("title") title: String): List<Movie> But got an exception org.springframework.data.mapping.MappingException: Could not read value rental_movie from result set! The example project on GitHub [LINK] . P.S I am qui
Cites woodstock · 2020-09 · Stack Overflow · on “How to do one to many relationships in JDBC?”
So I've been working recently with Spring JDBC myself. This is something similar with what I've come up with by reading here and there. I really appreciate the simplicity of JDBC combined with the domain driven approach: public class Teacher { @Id private int id; private String name; @MappedCollection(idColumn = "teacher_id", keyColumn = "teacher_key") private List<Class> classes; //setters and getters } public class Class { private int id; private String className; //setters and getters } Then you need to create the repository interface by extending CrudRepository from Spring JDBC, you don't need to create an extra repository to e.g. save Class data through Teacher : interface TeacherReposi