多对多关联在操作和性能方面都不太理想,所以多对多的映射使用较少,实际使用中最好转换成一对多的对象模型;Hibernate会为我们创建中间关联表,转换成两个一对多。
下面是一个教师与学生多对多的关联例子:
从上图中,Hibernate为我们创建了中间关联表Teacher_Student
package org.hibernate.domain;
import java.util.Set;
public class Teacher {
private int id;
private String name;
private Set<Student> students;
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;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
package org.hibernate.domain;
import java.util.Set;
public class Student {
private int id;
private String name;
private Set<Teacher> teachers;
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;
}
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
}
映射文件:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="org.hibernate.domain"> <class name="Teacher"> <id name="id"> <generator class="native" /> </id> <property name="name" /> <!-- teacher_student为临时表, teacher_id与student_id为临时表中字段 --> <set name="students" table="teacher_student" inverse="true"> <key column="teacher_id" /> <many-to-many class="Student" column="student_id" /> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="org.hibernate.domain"> <class name="Student"> <id name="id"> <generator class="native" /> </id> <property name="name" /> <set name="teachers" table="teacher_student"> <key column="student_id"/> <many-to-many class="Teacher" column="teacher_id"/> </set> </class> </hibernate-mapping>
测试类:
package org.hibernate.test;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.domain.Student;
import org.hibernate.domain.Teacher;
import org.hibernate.util.HibernateUtil;
import junit.framework.TestCase;
public class ManyToManyTest extends TestCase {
public void testAdd() {
Session session = null;
Transaction tx = null;
try {
Teacher teacher1 = new Teacher();
teacher1.setName("teacher1 name");
Teacher teacher2 = new Teacher();
teacher2.setName("teacher2 name");
Student student1 = new Student();
student1.setName("student1 name");
Student student2 = new Student();
student2.setName("student2 name");
session = HibernateUtil.getSession();
tx = session.beginTransaction();
session.save(teacher1);
session.save(teacher2);
session.save(student1);
session.save(student2);
tx.commit();
} finally {
if (session != null)
session.close();
}
}
public void testAddAssociation() {
Session session = null;
Transaction tx = null;
try {
Set<Teacher> teachers = new HashSet<Teacher>();
Teacher teacher1 = new Teacher();
teacher1.setName("teacher1 name");
Teacher teacher2 = new Teacher();
teacher2.setName("teacher2 name");
teachers.add(teacher1);
teachers.add(teacher2);
Set<Student> students = new HashSet<Student>();
Student student1 = new Student();
student1.setName("student1 name");
Student student2 = new Student();
student2.setName("student2 name");
students.add(student1);
students.add(student2);
teacher1.setStudents(students);
teacher2.setStudents(students);
student1.setTeachers(teachers);
student2.setTeachers(teachers);
session = HibernateUtil.getSession();
tx = session.beginTransaction();
session.save(teacher1);
session.save(teacher2);
session.save(student1);
session.save(student2);
tx.commit();
} finally {
if (session != null)
session.close();
}
}
public void testQuery() {
Session s = null;
Transaction tx = null;
try {
testAddAssociation();
s = HibernateUtil.getSession();
tx = s.beginTransaction();
Teacher t = (Teacher) s.get(Teacher.class, 1);
System.out.println("students size : " + t.getStudents().size());
tx.commit();
} finally {
if (s != null)
s.close();
}
}
}
测试testAdd方法,控制台打印的结果:
从结果看出,此时Teacher与Student之间并没有建立关系
测试testAddAssociation方法,控制台打印的结果:
从结果看出,此时Teacher与Student之间已经建立起了关系,体现在中间表teacher_student中
测试testQuery方法,控制台打印的结果:
从结果看出,从由Teacher获取学生的信息时,是先查询teacher表中的teacher_id值,然后在根据teacher_id值从中间表teacher_student与student中进行连接进而查询出学生的信息