【发布时间】:2019-07-21 07:30:08
【问题描述】:
我的 spring data jdbc 有如下映射问题:Student entity:
@Value(staticConstructor = "of")
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 @Wither
Set<StudentSubjectRegistration> studentSubjectRegistrations;
}
接下来我有一个StudentSubjectRegistration 实体:
@Value(staticConstructor = "of")
public class StudentSubjectRegistration {
@NotNull
private String electiveType;
@NotNull
private LocalDateTime created;
@NotNull
private long subjectid;
}
当我运行测试以找到一个特定的学生时,如下所示:
@Test
public void findOneStudent() throws InterruptedException, ExecutionException {
long studentId = 2L;
Future<Student> student = studentService.findById(2L);
while (!student.isDone()) {
Thread.sleep(100);
}
assertThat(student.get()).isNotNull();
assertThat(student.get().getStudentId()).isEqualTo(studentId);
}
控制台说有一条sql语句发出:
Executing prepared SQL statement [SELECT student.studentid AS studentid, student.userid AS userid, student.matriculationnumber AS matriculationnumber, student.email AS email FROM student WHERE student.studentid = ?]
就我的理解而言,应该发出另一个 sql 语句来获取关联的注册,不是吗?
相反,发生了异常:
java.lang.IllegalStateException: Required identifier property not found for class de.thd.awp.student.StudentSubjectRegistration!
我必须用@Id 对StudentSubjectRegistration 建模吗?
StudentSubjectRegistration 不应是聚合根。它应该是来自保存其注册的Student 的引用。
我错过了什么吗?
请进一步注意我在这篇博文https://spring.io/blog/2018/09/27/what-s-new-in-spring-data-lovelace 的启发下尝试对实体不变性进行建模...
我做的对吗?
感谢您的帮助!
【问题讨论】:
-
不可变的东西对我来说很好。
标签: java spring spring-data mapping spring-data-jdbc