Hibernate通过java注释将java中的嵌套集合映射到映射表?

发布于 2024-09-25 16:37:22 字数 666 浏览 4 评论 0原文

我在使用 java 注释编写 hibernate 映射来解决问题时遇到困难,如下所示。

问题:

表格

Table:Courses_Teachers
columns:
id
course_id
teacher_id
year(academic year)
Table: Courses_Students
columns:
id
course_id
student_id
year(academic year)
Table: Courses_Teachers_Students
course_teacher_id
course_student_id 

课程

class Student {
    Map<Course,List<Teacher>> courseTeachersMap;  
}

要求

  • 一门课程可以由多名教师向同一学生组教授。
  • 一门课程被分配到一个年级。
  • 一个年级可以有多个学生组,为课程分配不同的教师组。

请建议我如何在学生班级中为 courseTeachersMap 属性指定注释。

I am facing difficulty to write hibernate mapping with java annotation for problem as given below.

Problem:

Tables

Table:Courses_Teachers
columns:
id
course_id
teacher_id
year(academic year)
Table: Courses_Students
columns:
id
course_id
student_id
year(academic year)
Table: Courses_Teachers_Students
course_teacher_id
course_student_id 

Classes

class Student {
    Map<Course,List<Teacher>> courseTeachersMap;  
}

Requirements

  • A course can be taught by multiple teachers to same student group .
  • A course is assigned to a grade level.
  • A grade level can have multiple student groups who are assigned different set of teachers for a course.

Please suggest me how to specify annotation for courseTeachersMap property in class student.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没结果 2024-10-02 16:37:22

可以使用 Map 映射 ManyToMany 关联,这种映射的典型用例是当您具有三元关联时。例如,如果您有:

STUDENT_TEACHER_COURSE
STUDENT_ID(FK, PK)
TEACHER_ID(FK, PK)
COURSE_ID (FK, PK)

那么您可以定义以下映射(假设您使用的是 Hibernate Annotations <3.5):

@Entity
public class Student {
    ...
    @ManyToMany
    @JoinTable(
        name="STUDENT_TEACHER_COURSE", 
        joinColumns= { @JoinColumn(name="STUDENT_ID") },
        inverseJoinColumns= { @JoinColumn(name="TEACHER_ID") }
    )
    @MapKeyManyToMany(joinColumns = @JoinColumn(name="COURSE_ID",unique = false))
    protected Map<Course,Teacher> teachers ;
    ...
}

但是我认为您不能有一个“嵌套”List; 作为 Map 内的值,我认为 Hibernate 无法映射它,我会考虑从实体获取 List

参考资料

资源

It is possible to map a ManyToMany association using a Map, and a typicall use case for this kind of mapping is when you have a ternary association. For example, if you have:

STUDENT_TEACHER_COURSE
STUDENT_ID(FK, PK)
TEACHER_ID(FK, PK)
COURSE_ID (FK, PK)

Then you could define the following mapping (assuming you're using a Hibernate Annotations < 3.5):

@Entity
public class Student {
    ...
    @ManyToMany
    @JoinTable(
        name="STUDENT_TEACHER_COURSE", 
        joinColumns= { @JoinColumn(name="STUDENT_ID") },
        inverseJoinColumns= { @JoinColumn(name="TEACHER_ID") }
    )
    @MapKeyManyToMany(joinColumns = @JoinColumn(name="COURSE_ID",unique = false))
    protected Map<Course,Teacher> teachers ;
    ...
}

But I don't think you can have a "nested" List<Teacher> as value inside the Map, I don't think Hibernate can map that and I'd consider getting the List from an entity instead.

References

Resources

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文