hibernate的多对多例子讲解(加图片)
在hibernate中也有多对多的关系。但是这样关系执行的效率不高,所以我们可以通过两个多对1或者两个1对多来实现。
在现实生活中多对多的关系也比较常见。比如说老师和学生。一个老师有多个学生,一个学生也有多个老师。
如何实现老师和学生的关系我们可以建立一个中间表。中间表的作用就是关联老师和学生用的。
大家请看这个表:
这个3个表就是这种关系。现在我来写一个实例来实现以上的关系。
1teacher类
package com.fish.testdao;
import java.util.Set;
public class Teacher {
private int id;
private String name;
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;
}
}
2student类
package com.fish.testdao;
import java.util.Set;
public class Student {
private int id;
private String name;
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;
}
}
3.学生xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/HibernateMapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.fish.testdao.Student">
<id name="id" type="integer">
<generator class="increment"></generator>
</id>
<property name="name"></property>
<set name="teachers" table="teacher_student">//我们会产生一个老师学生关联表
<key column="student_id"></key>//然后我们会在这个表中产生一个student_id的字段
<many-to-many class="com.fish.testdao.Teacher"column="teacher_id"></many-to-many>
</set>
</class>
</hibernate-mapping>
*注set就是用来做中间表的关联的。其实这个在一个表中有就可以了。这个set可以在techer的xml省去。当然写上也不会有错。
4.techer的xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/HibernateMapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.fish.testdao.Teacher">
<id name="id" type="integer">
<generator class="increment"></generator>
</id>
<property name="name"></property>
<set name="students" table="teacher_student">
<key column="teacher_id"></key>
<many-to-many class="com.fish.testdao.Student"column="student_id" />
</set>
</class>
</hibernate-mapping>
5.我们写一个测试方法。
packagecom.fish.domain;
importjava.util.HashSet;
import java.util.List;
import java.util.Set;
importorg.hibernate.Query;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
importcom.fish.testdao.Student;
importcom.fish.testdao.Teacher;
public class Test4 {
public static Session getMySession() {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
SessionFactory factory =configuration.buildSessionFactory();
Session session = factory.openSession();
return session;
}
//向3个表添加数据,这个方法里面我打了注释。因为如果你同时向中间表里面加数据,那肯定会报错的。试想两个set都是描述的是一种关系。那么同时插入一定会报错。
public static void add() {
// Set<Student> setStu = newHashSet<Student>();
Set<Teacher> setTea = newHashSet<Teacher>();
Teacher teacher = new Teacher();
Student student = new Student();
student.setName("qwe");
teacher.setName("黄老师");
// setStu.add(student);
setTea.add(teacher);
student.setTeachers(setTea);
// teacher.setStudents(setStu);
Session session = getMySession();
Transaction transaction = session.beginTransaction();
transaction.begin();
session.save(student);
session.save(teacher);
transaction.commit();
session.close();
}
public static void query() {
String hql = "from Teacher";
Session session = getMySession();
Query query = session.createQuery(hql);
List<Teacher> list = query.list();
for (Teacher i : list) {
System.out.println(i.getName() + "有学生:");
for (Student j : i.getStudents()) {
System.out.print(j.getName());
}
System.out.println();
}
session.close();
}
public static void main(String[] args) {
query();
}
}
接着我给你们看看这三个表的数据库中的数据。
查询结果是: