I want to model a OneToMany Relation with Spring Data JDBC. I´ve read on this very useful blog https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates that you should use references when you want to model ToMany Reference:
Therefore any Many-to-One and Many-to-Many relationship must be modeled by just referencing the id.
So I have this scenario:
One Student
can have multiple Registration
. And one Registration
can have exactly one Student
. If you delete Registration
the assigned Student
should not get deleted cascading.
I ended up with this modelling:
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
public class Registration {
private final @Id
@Wither
long registrationId;
@NotNull
private String electiveType;
@NotNull
private LocalDateTime created = LocalDateTime.now();
@NotNull
private StudentRegistrationReference studentRegistrationReference;
}
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
public class StudentRegistrationReference {
private long student;
private long registration;
}
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
public class Student {
private final @Id
@Wither
long studentId;
@NotNull
@Size(min = 4, max = 20)
private String userId;
@NotNull
@Min(0)
private int matriculationNumber;
@NotNull
@Email
private String eMail;
private Set<StudentRegistrationReference> studentRegistrationReferences = new HashSet<>();
}
My question is whether my modeling is correctly implemented?
Student
by theIStudentRepostory
will theSet<RegistrationRef>
being fetched with theRegistration
Ids? 2. You have mentioned table generated/generation in the above answer. How does that work out with Spring Data JDBC? – Denisdenise