一、多對多單向關聯
假設一個老師教多個學生,一個學生被多個老師教,這就是典型的多對多關系
配置方式是在Teacher類的getStudents()方法上添加注解@ManyToMany
@JoinTable(name="t_s" ,
joinColumns = { @JoinColumn(name="teacher_id")},
inverseJoinColumns = { @JoinColumn(name="student_id")}
)
Teacher類:
package cn.orlion.hibernate.model; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; @Entity public class Teacher { private int id; private String name; private Set<Student> students = new HashSet<Student>(); @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @ManyToMany @JoinTable(name="t_s" , // t_s是生成的中間表的表名 joinColumns = { @JoinColumn(name="teacher_id")},// teacher_id是生成中間表中參考自Teacher的外鍵 inverseJoinColumns = { @JoinColumn(name="student_id")}// inverseJoinColumns配置的是反轉的對象(即Student)的idstudent_id是中間表中參考自Student的外鍵 ) public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } }
Student類:
package cn.orlion.hibernate.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Student { private int id; private String name; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
生成的sql語句:
create table Student ( id integer not null auto_increment, name varchar(255), primary key (id) ) create table Teacher ( id integer not null auto_increment, name varchar(255), primary key (id) ) create table t_s ( teacher_id integer not null, student_id integer not null, primary key (teacher_id, student_id) ) alter table t_s add constraint FK_3no0wm9bwtogr87t4gvd4t3x5 foreign key (student_id) references Student (id) alter table t_s add constraint FK_pj2fmd6skwfi7jm2avlo4n6mf foreign key (teacher_id) references Teacher (id)
二、多對多雙向關聯
單向關聯可一理解為:老師知道教的學生的名字,但是學生不知道教的多個老師。
雙向關聯可以理解為:老師知道教的學生的名字,學生也知道教的多個老師。
配置方式:Teacher類與單向關聯相同,Student類添加Set<Teacher> teachers屬性,在getTeaches()方法上添加注解@ManyToMany(mappedBy="students")
Teacher類:
package cn.orlion.hibernate.model; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; @Entity public class Teacher { private int id; private String name; private Set<Student> students = new HashSet<Student>(); @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @ManyToMany @JoinTable(name="t_s" , // t_s是生成的中間表的表名 joinColumns = { @JoinColumn(name="teacher_id")}, inverseJoinColumns = { @JoinColumn(name="student_id")} ) public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } }
Student類:
package cn.orlion.hibernate.model; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Student { private int id; private String name; private Set<Teacher> teachers = new HashSet<Teacher>(); @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @ManyToMany(mappedBy="students") public Set<Teacher> getTeachers() { return teachers; } public void setTeachers(Set<Teacher> teachers) { this.teachers = teachers; } }
生成的sql語句:
create table Student ( id integer not null auto_increment, name varchar(255), primary key (id) ) create table Teacher ( id integer not null auto_increment, name varchar(255), primary key (id) ) create table t_s ( teacher_id integer not null, student_id integer not null, primary key (teacher_id, student_id) ) alter table t_s add constraint FK_3no0wm9bwtogr87t4gvd4t3x5 foreign key (student_id) references Student (id) alter table t_s add constraint FK_pj2fmd6skwfi7jm2avlo4n6mf foreign key (teacher_id) references Teacher (id)