annotation方式:
一。编写实体类,双向关联则应双方都有装对方的容器
@Entity public class Student{ private int id; private String name; private Set<Teacher> students =new HashSet<Teacher>(); @Id @GeneratedValue public int getId() { return id; } @ManyToMany(mappedBy="students") public Set<Teacher> getStudents() { return students; } public void setStudents(Set<Teacher> students) { this.students = students; } public String getName() { return name; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } }
注意配置mappedBy,以对方类属性为主导
二。
@Entity public class Teacher { private int id; private String name; private Set<Student> students =new HashSet<Student>(); @Id @GeneratedValue public int getId() { return id; } @ManyToMany @JoinTable(name="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; } public String getName() { return name; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } }
设置注释@ManyToMany,@JoinTable则设置中间表,name设置名称,joinColumns设置中间表列指向当前类ID,inverseJoinColumns设置中间表列指向对方类ID
三。设置hibernate.cfg.xml
<mapping class="org.hibernate.tutorial.domain.Teacher"/> <mapping class="org.hibernate.tutorial.domain.Student"/>
xml方式:
配置双方xml文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="org.hibernate.tutorial.domain"> <class name="Teacher" > <id name="id" column="teacherId"> <generator class="native"/> </id> <property name="name"></property> <set name="students" table="t_s"> <key column="teacher_Id"></key> <many-to-many class="org.hibernate.tutorial.domain.Student" column="student_Id"/> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="org.hibernate.tutorial.domain"> <class name="Student" > <id name="id" column="studentId"> <generator class="native"/> </id> <property name="name"></property> <set name="students" table="t_s"> <key column="student_Id"></key> <many-to-many class="org.hibernate.tutorial.domain.Student" column="teacher_Id"/> </set> </class> </hibernate-mapping>
双方配置集合属性,name配置本类属性,table配置中间表名(双方必一致,否则产生两张表),key配置中间表列指向本类ID。
many-to-many 配置包含类,column配置中间表列指向对方表ID
测试结果
create table Student ( studentId integer not null auto_increment, name varchar(255), primary key (studentId) ) create table Teacher ( teacherId integer not null auto_increment, name varchar(255), primary key (teacherId) ) create table t_s ( teacher_Id integer not null, student_Id integer not null, primary key (student_Id, teacher_Id) ) alter table t_s add index FK_t2s4kp3kjctswb718yn0ny9ck (student_Id), add constraint FK_t2s4kp3kjctswb718yn0ny9ck foreign key (student_Id) references Student (studentId) alter table t_s add index FK_q8pyhjtlr4wxothocdby56ttk (teacher_Id), add constraint FK_q8pyhjtlr4wxothocdby56ttk foreign key (teacher_Id) references Teacher (teacherId) alter table t_s add index FK_q8pyhjtlr4wxothocdby56ttk (teacher_Id), add constraint FK_q8pyhjtlr4wxothocdby56ttk foreign key (teacher_Id) references Student (studentId)