开发者

Hibernate Mapping of nested collection in java to mapping table through java annotation?

开发者 https://www.devze.com 2023-01-18 17:28 出处:网络
I am facing difficulty to write hibernate mapping with java annotation for problem as given below. Problem:

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)
T开发者_如何学Pythonable: 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.


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

  • Hibernate Annotations 3.4 Reference Guide
    • 2.4.6.2.2. Map
  • Hibernate Annotations 3.5 Reference Guide
    • 2.2.5.3.4. Indexed collections (List, Map)

Resources

  • @MapKeyManyToMany and value column name
  • Help with annotations -- Map
0

精彩评论

暂无评论...
验证码 换一张
取 消