如何使用Annotations将列表作为值保存?

问题描述 投票:0回答:1

我有三个模型类,学生,工作场所,周,我试图坚持使用Annotations。每个学生每周可以有多个工作场所,所以学生班有一个字段Map<Week, List<Workplace>>。但我找不到将其映射到数据库的解决方案。

我确实设法使用@ManyToMany与Student_id,Workplace_id和Week_id作为数据库表中的列来保持学生和工作场所之间的JoinTablerelationship,但是这个地图只是学生类中的字段Map<Week, Workplace>

这是我上次尝试过的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
}

谢谢你的帮助!

java mysql hibernate jpa
1个回答
0
投票

没有JPA不支持这一点。请参阅此文档https://en.wikibooks.org/wiki/Java_Persistence/Relationships#Nested_Collections.2C_Maps_and_Matrices,它解释了原因。

并且已经在这个问题I want to map a Map<Long, List<POJO>> through JPA中提出并回答了问题

© www.soinside.com 2019 - 2024. All rights reserved.