我有一个spring data jdbc的映射问题如下:
Student
实体:
@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!
我必须StudentSubjectRegistration
用一个建模@Id
吗?
StudentSubjectRegistration
不应该是聚合根。它应该Student
是保存其注册的参考。
我错过了什么吗?
另外请注意,我在这篇博文https://spring.io/blog/2018/09/27/what-s-new-in-spring-data-lovelace的启发下尝试对实体不变性进行建模...
我做对了吗?
谢谢你的帮助!