1

我有一个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的启发下尝试对实体不变性进行建模...

我做对了吗?

谢谢你的帮助!

4

1 回答 1

0

聚合中的实体需要人们可能称之为的东西effective-id,它在聚合中必须是唯一的。

在 和 的情况下List,这些是通过对聚合根和 / 的索引/Map键的反向引用来构建的。ListMap

Set没有键,索引或类似的,因此,注释的属性是@Id必要的。

在您的情况下,假设不能两次注册同一主题,这subjectid听起来像是一个有前途的候选人。Student

于 2019-02-28T06:52:14.617 回答