首页 > 代码库 > 【SSH三大框架】Hibernate基础第八篇:多对多关联关系的操作

【SSH三大框架】Hibernate基础第八篇:多对多关联关系的操作

在Hibernate中的多对多关联关系,一般是不会使用的,因为对于数据库查询的时候时间复杂度太高。

我们在这里做的是学生和老师,一个学生可以有多个老师,一个老师可以有多个学生。




我们首先建立一个学生实体类:Student.java

package cn.itcast.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;
	}
}
我们定义了三个三个属性,分别是id、name和一个set集合


然后是老师实体类:Teacher.java

package cn.itcast.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;
	}
}
我们仍然定义了三个实体类,id、name和一个set集合


然后,我们看下Stduent类的映射文件:

<?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="cn.itcast.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>  
在这个文件中,我们定义了:id自动增长、name属性,还有一个<set>标签,name属性是Student.java中的set集合的那个属性,然后我们在<key>标签定义外键是student_id,然后又定义了一个<many-to-many>标签,规定了多对多的关系。


下边是Teacher.java类的映射文件:

<?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="cn.itcast.hibernate.domain">  
    <class name="Teacher">  
        <id name="id">  
            <generator class="native"/>   
        </id>  
        <property name="name" />  
       	<set name="students" table="teacher_student">
       		<key column="teacher_id" />
       		<many-to-many class="Student"  column="student_id"/>
       	</set>
    </class>  
      
</hibernate-mapping>  
和上边的Stduent类的映射文件差不多


然后,我们写一个测试类:Many2Many.java

package cn.itcast.hibernate;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.itcast.hibernate.domain.Student;
import cn.itcast.hibernate.domain.Teacher;

public class Many2Many {
	public static void main(String[] args) {
		add();
		query(1);
	}

	
	static void add(){
		Session s = null;
		Transaction tx = null;
		try{
			//定义了一个Teahcer的set集合
			Set<Teacher> ts = new HashSet<Teacher>();
			//定义了一个Student的set集合
			Set<Student> ss = new HashSet<Student>();
			//增加一个老师1
			Teacher t1 = new Teacher();
			t1.setName("t1 name");
			ts.add(t1);
			//增加一个老师2
			Teacher t2 = new Teacher();
			t2.setName("t2 name");
			ts.add(t2);
			//增加一个学生1
			Student s1 = new Student();
			s1.setName("s1");
			ss.add(s1);
			//增加一个学生2
			Student s2 = new Student();
			s2.setName("s2");
			ss.add(s2);
			
			//这里是设置两个teacher的set属性
			t1.setStudents(ss);
			t2.setStudents(ss);
			
			/*
			 * 这是是设置两个student的set属性,如果和上边的设置同时出现,则会抛出异常,因为多对多的关系在上边已经建立
			 * s1.setTeachers(ts);
			   s2.setTeachers(ts);
			   
			*/
			
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			
			s.save(t1);
			s.save(t2);
			s.save(s1);
			s.save(s2);
			
			tx.commit();
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}
	
	static void query(int id){
		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			//根据id查询得到Teacher对象
			Teacher t = (Teacher) s.get(Teacher.class, id);
			//把Teacher对象对应的学生的数量打出来
			System.out.println("Students:"+t.getStudents().size());
			tx.commit();
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}
}








【SSH三大框架】Hibernate基础第八篇:多对多关联关系的操作