Hibernate的多对多映射关系

example:
    老师(teacher)和学生(Student)就是一个多对多的关系吧?老师可以有多个学生,学生也可以由多个老师,那在Hibernate中多对多是怎样实现的呢??

在Hibernate中多对多关系分为两种:1单向的多对多,2双向的多对多

下面详细说明一些两种有什么不同和实现步骤

  I单向的多对多实现步骤:

  1新建teacher、student表,可以任意选择在其中一个表添加另一个表的集合,(例如在teacher中添加student的set<Student>集合,例如private set<student> students)

  代码如下:

  teacher:

package com.atguigu.hibernate.n2n;

import java.util.Set;

public class Teacher {
    private Integer ID;
    private String  Name;
    private Set<Student> students;
    public Integer getID() {
        return ID;
    }
    public void setID(Integer iD) {
        ID = iD;
    }
    public String getTea_Name() {
        return Name;
    }
    public void setTea_Name(String Name) {
        this.Name =Name;
    }
    public Set<Student> getStudents() {
        return students;
    }
    public void setStudents(Set<Student> students) {
        this.students = students;
    }
    
}

Student:
  

package com.atguigu.hibernate.n2n;

public class Student {
    private Integer S_ID;
    private String  s_Name;
    public Integer getS_ID() {
        return S_ID;
    }
    public void setS_ID(Integer s_ID) {
        S_ID = s_ID;
    }
    public String getS_Name() {
        return s_Name;
    }
    public void setS_Name(String s_Name) {
        this.s_Name = s_Name;
    }
    
}

2在有set集合属性的持久化类的.hbm.xml文件里添加集合信息和many-to-many信息,另外一个表的.hbm.xml文件跟平常一样即可

teacher:

  

<?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>
    <class name="com.atguigu.hibernate.n2n.Teacher" table="TEACHER">
      
        <id name="ID" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        
        <property name="Name" type="java.lang.String" access="field">
            <column name="NAME" />
        </property>
          <!-- 
                name:set集合的名字
                Table:中间表的名字
                key--column:中间表的外键列的名字,这个类的值就是属于<class name="">这个类的ID值
                many-to-many的column:也是指定中间表的外键列的名字,这列的值就属于many-to-many的class类的ID值
         -->
        <!---注释部分可能会引起XML格式错误,--->
        <set name="students" table="t_s_Connection">
            <key>
                <column name="T_ID" />
            </key>
            <many-to-many class="com.atguigu.hibernate.n2n.Student"  column="S_ID"/>
        </set>
        
    </class>
</hibernate-mapping>
    

student:

  

<?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>
    <class name="com.atguigu.hibernate.n2n.Student" table="STUDENT">
      
        <id name="S_ID" type="java.lang.Integer">
            <column name="S_ID" />
            <generator class="native" />
        </id>
       
        <property name="s_Name" type="java.lang.String">
            <column name="S_NAME" />
       </property>
       
    </class>
</hibernate-mapping>

然后配置一下Hibernate.cfg.xml文件即可

单向的多对多关系,就是这样了,但是其的增删改查又有什么特点???这个稍后再探究

 

 

II双向的多对多关系如何配置
1在双方持久化类(就是teacher和student)中互相添加对方的对象集合,并且当中有一个需要实例化

例如:在teacher中添加private set<student>

    在student中添加private set<teacher>=new hashSet<teacher>();

代码:teacher:

package com.atguigu.hibernate.n2n;

import java.util.Set;

public class Teacher {
    private Integer ID;
    private String  Name;
    private Set<Student> students;
    public Integer getID() {
        return ID;
    }
    public void setID(Integer iD) {
        ID = iD;
    }
    public String getTea_Name() {
        return Name;
    }
    public void setTea_Name(String Name) {
        this.Name =Name;
    }
    public Set<Student> getStudents() {
        return students;
    }
    public void setStudents(Set<Student> students) {
        this.students = students;
    }
    
}

student:

package com.atguigu.hibernate.n2n;

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

public class Student {
    private Integer S_ID;
    private String  s_Name;
    private Set<Teacher> teachers=new HashSet<Teacher>();
    
    public Set<Teacher> getTeachers() {
        return teachers;
    }
    public void setTeachers(Set<Teacher> teachers) {
        this.teachers = teachers;
    }
    public Integer getS_ID() {
        return S_ID;
    }
    public void setS_ID(Integer s_ID) {
        S_ID = s_ID;
    }
    public String getS_Name() {
        return s_Name;
    }
    public void setS_Name(String s_Name) {
        this.s_Name = s_Name;
    }
    
}

2在双方的持久化类中添加set和many-to-many设置

teacher.hbm.xml:

<?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>
    <class name="com.atguigu.hibernate.n2n.Teacher" table="TEACHER">
      
        <id name="ID" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        
        <property name="Name" type="java.lang.String" access="field">
            <column name="NAME" />
        </property>
          <!-- 
                name:set集合的名字
                Table:中间表的名字
                key--column:中间表的外键列的名字(可以放任意名字),就是指明中间表的外键列是当前持久化类的的ID属性
             many-to-many的column:也是指定中间表的外键列的名字,这列的值就属于many-to-many的class类的ID值
         -->
        <set name="students" table="t_s_Connection">
            <key>
                <column name="T_ID" />
            </key>
            <many-to-many class="com.atguigu.hibernate.n2n.Student"  column="S_ID"/>
        </set>
        
    </class>
</hibernate-mapping>

student.hbm.xml:

<?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>
    <class name="com.atguigu.hibernate.n2n.Student" table="STUDENT">
      
        <id name="S_ID" type="java.lang.Integer">
            <column name="S_ID" />
            <generator class="native" />
        </id>
       
        <property name="s_Name" type="java.lang.String">
            <column name="S_NAME" />
       </property>
       <set name="teachers" table="t_s_Connection">
               <key>
                   <column name="S_ID"></column>
                   
               </key>
               <many-to-many class="com.atguigu.hibernate.n2n.Teacher" column="T_ID"></many-to-many>
       </set>
    </class>
</hibernate-mapping>

然后在Hibernate.cfg.xml文件配置一下mapping-resource即可

 

 

但是双向的多对多关系的增删改查又是怎样的呢??

 

 

而且单向的多对多关系和多向的多对多关系两者有什么不同,有什么有缺点??

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。