我有三个模型类,Student、Workplace、Week,我尝试坚持使用Annotations
. 每个 Student 每周可以有多个工作场所,因此 Student 类有一个字段Map<Week, List<Workplace>>
。但我没有找到将其映射到数据库的解决方案。
我确实设法@ManyToMany
使用JoinTable
Student_id、Workplace_id 和 Week_id 作为 DB 表中的列来保持Student 和 Workplace 之间的关系,但映射的内容只是Map<Week, Workplace>
Student 类中的一个字段。
这是Annotations
我上次尝试的简化模型类。我得到的错误是Use of @OneToMany or @ManyToMany targeting an unmapped class: model.Student.studentsWorkplaces[java.util.List]
。
@Entity
public class Student {
@Id
private Integer id;
@ManyToMany
@JoinTable(name = "STUDENTS_WORKPLACES",
joinColumns = @JoinColumn(name = "STUDENT"),
inverseJoinColumns = @JoinColumn(name = "WEEK"))
@MapKeyJoinColumn(name = "WORKPLACE")
private Map<Week, List<Workplace>> studentsWorkplaces;
// Getters, Setters
}
@Entity
public class Workplace {
@Id
private Integer id;
@ManyToMany(mappedBy="studentsWorkplaces")
private Map<Week, Student> workplaceStudents;
// Getters, Setters
}
@Entity
public class Week {
@Id
private String week_year;
@ManyToMany(mappedBy="studentsWorkplaces")
private List<Student> students;
@ManyToMany(mappedBy="studentsWorkplaces")
private List<Workplace> workplaces;
// Getters, Setters
}
谢谢你的帮助!
没有 JPA 不支持。请参阅此文档https://en.wikibooks.org/wiki/Java_Persistence/Relationships#Nested_Collections.2C_Maps_and_Matrices,其中解释了原因。
并且已经在这个问题中提出并回答了我想通过 JPA 映射 Map<Long, List<POJO>>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句