双向多对多关系的映射

来源:互联网 发布:java finalize 编辑:程序博客网 时间:2024/05/22 13:14

          老师和学生有时候也属于多对多的关系,一个老师可以教多个学生,一个学生可以被多个老师教授。

    当两个表是多对多关系的时候,我们需要建一个连接表,把这两个表的关系保存在其中。

   表关系如下:

   


     学生类:

     

package com.shizhan.po;import java.util.HashSet;import java.util.Set;public class Student {private Long  sid ;    private String name ;    private Set<Teacher> teachers = new HashSet<Teacher>();    public Long getSid() {return sid;}public void setSid(Long sid) {this.sid = sid;}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;}    }

   老师类:

  

package com.shizhan.po;import java.util.HashSet;import java.util.Set;public class Teacher {    private Long tid ;    private String name ;    private Set<Student> students =new HashSet<Student>();    public Long getTid() {return tid;}public void setTid(Long tid) {this.tid = tid;}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;}        }

   老师配置文件:

   

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>    <class name="com.shizhan.po.Teacher" table="TEACHER" schema="SCOTT">        <id name="tid" type="java.lang.Long">            <column name="TID" precision="10" scale="0" />            <generator class="increment" />        </id>         <property name="name" type="java.lang.String">            <column name="NAME" length="20" />        </property>                <set name="students" table="teacherstudent">           <key column="TID"/>           <many-to-many class="com.shizhan.po.Student" column="SID"/>        </set>    </class></hibernate-mapping>


      <set name="students" table="teacherstudent">
           <key column="TID"/>
           <many-to-many class="com.shizhan.po.Student" column="SID"/>
        </set>

      因为多对多 关系 多了一张表,teacherstudent,所以老师要获取学生的信息,需要先去teacherstudent表中查找学生id,那么什么样的学生id符合要求呢,就是tid等于老师tid的学生id。所以 <key column="TID"/>必须指明列为tid。

     现在获取的只是学生id,那么要获取学生,还需要去学生表中查找,以column="SID"为依据去查找符合条件的学生。


    学生配置文件:

    

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>    <class name="com.shizhan.po.Student" table="STUDENT" schema="SCOTT">        <id name="sid" type="java.lang.Long">            <column name="SID" precision="10" scale="0" />            <generator class="increment" />        </id>         <property name="name" type="java.lang.String">            <column name="NAME" length="20" />        </property>                <set name="teachers" table="teacherstudent">           <key column="SID"/>           <many-to-many class="com.shizhan.po.Teacher" column="TID"/>        </set>    </class></hibernate-mapping>


   测试代码:

   设置学生的老师集合,或者设置老师的学生集合时,只需要告诉老师,他有哪些学生,或者告诉学生,他有哪些老师就好,如果双方都告诉的话,那么他们都会试图往连接表中插入数据,就会出错

package com.shizhan.test;import java.util.HashSet;import java.util.Set;import org.hibernate.Session;import org.hibernate.Transaction;import com.shizhan.po.Student;import com.shizhan.po.Teacher;import com.shizhan.util.DBUtil;public class Test {    public static void main(String[] args) {         Session session =  DBUtil.getSession();  Transaction tx = session.beginTransaction();     try{              Student s1 = new Student();     s1.setName("s1");        Student s2 = new Student();     s2.setName("s2");          Teacher t1 = new Teacher();     t1.setName("t1");        Teacher t2 = new Teacher();     t2.setName("t2");          Set<Student> students = new HashSet<Student>();     students.add(s1);     students.add(s2);       Set<Teacher> teachers = new HashSet<Teacher>();  teachers.add(t1);  teachers.add(t2);    //告诉老师,他有哪些学生,或者学生,他有哪些老师就好,如果双方都告诉的话,那么他们都会试图往连接表中插入数据,就会出错  //t1.setStudents(students);  // t2.setStudents(students);      s1.setTeachers(teachers);  s2.setTeachers(teachers);    session.save(t1);  session.save(t2);  session.save(s1);  session.save(s2);  tx.commit();     }catch(Exception e)     {     e.printStackTrace();     tx.rollback();     }finally{      session.close();     }}}