Hibernate的关联映射,以下为类的关系
Teacher和Student为一对多双向关联,配置时在多的一段维护关系
student:
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "teacher_id") public Teacher getTeacher() { return teacher; }
Teacher:
@OneToMany(mappedBy = "teacher")
public Set<Student> getStudents() { return students; }
Student和Group为多对多双向关联,这是需要 引入一个关联表,只需在任何一端维护关系即可,这里Student为关联主体
Student:
@ManyToMany
@JoinTable(name = "student_group", joinColumns = {@JoinColumn(name = "student_id")}, inverseJoinColumns = {@JoinColumn(name = "group_id")} ) public Set<Group> getGroups() { return groups; }
Group:
@ManyToMany(mappedBy = "groups")
public Set<Student> getStudents() { return students; }
Student与StudentCard为一对一双向关联,Student端坐位主体
Student:
@OneToOne(cascade = CascadeType.ALL,mappedBy="student")
public StudentCard getStudentCard() { return studentCard; }
StudentCard:
@OneToOne
public Student getStudent() { return student; }
这样这个关联关系就配置出来了。