Java 集合的映射需要Id吗?

Java 集合的映射需要Id吗?,java,spring,spring-data,mapping,spring-data-jdbc,Java,Spring,Spring Data,Mapping,Spring Data Jdbc,我对spring数据jdbc有如下映射问题: 学生实体: @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 @Em

我对spring数据jdbc有如下映射问题:
学生
实体:

@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;
}
@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);
}
@测试
public void findOneStudent()引发InterruptedException、ExecutionException{
长studentId=2L;
未来学生=studentService.findById(2L);
而(!student.isDone()){
睡眠(100);
}
assertThat(student.get()).isNotNull();
断言(student.get().getStudentId()).isEqualTo(studentId);
}
控制台显示发出了一条sql语句:
执行准备好的SQL语句[选择student.studentid作为studentid,选择student.userid作为userid,选择student.matriculationnumber作为matriculationnumber,选择student.email作为来自student的电子邮件,其中student.studentid=?][/code>

就我的理解而言,应该发布另一个sql语句来获取相关的注册,不是吗

相反,出现了一个异常:
java.lang.IllegalStateException:未找到de.thd.awp.student.StudentSubjectRegistration类所需的标识符属性

我是否必须使用
@Id
学生主体注册进行建模

StudentSubjectRegistration
不应是聚合根。它应该是保存注册的
学生
的参考

我错过什么了吗

此外,请注意,我试图建立实体不变性模型的灵感来自于这篇博文

我做得对吗


谢谢你的帮助

聚合中的实体需要可以称之为有效id的
,该id在聚合中必须是唯一的

列表
映射
的情况下,通过对
列表
/
映射
的聚合根和索引/键的反向引用来构建这些属性

使用
集合
时,没有键、索引或类似项,因此需要使用
@Id
注释属性


在您的情况下,
subjectid
听起来像是一个很有前途的候选人,假设一个
学生不能注册同一科目两次。

聚合中的实体需要一个可以称之为有效id的id,该id在聚合中必须是唯一的

列表
映射
的情况下,通过对
列表
/
映射
的聚合根和索引/键的反向引用来构建这些属性

使用
集合
时,没有键、索引或类似项,因此需要使用
@Id
注释属性


在你的情况下,
subjectid
听起来像是一个有前途的候选人,假设一个
学生不能注册同一科目两次。

不可变的东西对我来说很好。不可变的东西对我来说很好。谢谢Jens的帮助。我做了以下工作:
private final@Id long subject在my
学生主题注册中
。。。现在控制台显示:
由以下原因引起:org.postgresql.util.psqleexception:Der Spaltenname studentSubjectRegistrations\u subjectid wurde in diesem ResultSet nicht gefunden。
。。。我的命名不正确吗?这是这个bug的一个变体:它破坏了不可变的支持。因此,您当前不能将集合设为构造函数参数。谢谢。为了绕过这个问题,我使用了以下技巧:
@Setter private Set studentSubjectRegistrationsStudent
enttiy中使用code>。@ThomasLang更好的版本是使用
@Wither
,因为它保持了不变性。您将需要两个构造函数:一个包含所有参数。还有一个是带有
@PersistenceContext
注释的非实体的。谢谢Jens的帮助。我做了以下工作:
private final@Id long subject在my
学生主题注册中
。。。现在控制台显示:
由以下原因引起:org.postgresql.util.psqleexception:Der Spaltenname studentSubjectRegistrations\u subjectid wurde in diesem ResultSet nicht gefunden。
。。。我的命名不正确吗?这是这个bug的一个变体:它破坏了不可变的支持。因此,您当前不能将集合设为构造函数参数。谢谢。为了绕过这个问题,我使用了以下技巧:
@Setter private Set studentSubjectRegistrationsStudent
enttiy中使用code>。@ThomasLang更好的版本是使用
@Wither
,因为它保持了不变性。您将需要两个构造函数:一个包含所有参数。还有一个是用
@PersistenceContext
注释的非实体的。