我想用 Spring Data JDBC 建模 OneToMany 关系。我已经阅读了这个非常有用的博客https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates,当你想建模 ToMany 参考时,你应该使用参考:
因此,任何多对一和多对多关系都必须仅通过引用 id 来建模。
所以我有这种情况:
一个Student
可以有多个Registration
. 一个Registration
可以正好有一个Student
。如果您删除Registration
分配的Student
不应该被删除级联。
我最终得到了这个模型:
@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<>();
}
我的问题是我的建模是否正确实施?