码迷,mamicode.com
首页 > Web开发 > 详细

hibernate多对多详解

时间:2017-08-27 15:25:42      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:his   tor   todo   turn   nsa   自己的   any   com   exce   

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 package="com.hibernate.b_manytomany">
    
    <class name="Student" table="t_student">    
    <id name="id"  >
         <generator class="native"/> 
    </id>    
        <property name="studentName"/>
        <property name="studentNumber" ></property>
        <set name="teachers" table="student_teacher"> <!-- 关联属性teacher,创建中间表student_teacher -->
            <key column="student_id"></key><!-- 本方外键 -->
            <!-- many-to-many多对多,关联类Teacher,外键列teacher_id -->
            <many-to-many class="Teacher" column="teacher_id"></many-to-many>
        </set>
    </class>
    
</hibernate-mapping>

 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 package="com.hibernate.b_manytomany">
    
    <class name="Teacher" table="t_teacher">    
    <id name="id"  >
         <generator class="native"/> 
    </id>    
        <property name="teacherName"/>
        <!-- 
            set集合指定name为类元素student 中间表student_teacher
            key的column指定自己的外键为teacher_id列
            many-to-many 关联类Student 以及对方的外键student_id
         -->
        <set name="students" table="student_teacher">
            <key column="teacher_id"></key>
            <many-to-many class="Student" column="student_id"></many-to-many>
        </set>
    </class>
    
</hibernate-mapping>

 

------------------------------------------

Student.java

public class Student {
    int id;
    String studentName;
    String studentNumber;
    Set<Teacher> teachers=new HashSet<Teacher>();
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public String getStudentNumber() {
        return studentNumber;
    }
    public void setStudentNumber(String studentNumber) {
        this.studentNumber = studentNumber;
    }
    public Set<Teacher> getTeachers() {
        return teachers;
    }
    public void setTeachers(Set<Teacher> teachers) {
        this.teachers = teachers;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", studentName=" + studentName
                + ", studentNumber=" + studentNumber + "]";
    }
}

1、用set集合指定对多关系

2、tostring方法重写时不能包含另一个对象,否则回导致循环调用函数异常

-----------------------------------------

Teacher.java

public class Teacher {
    int id;
    String teacherName;
    Set<Student> students=new HashSet<Student>();
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTeacherName() {
        return teacherName;
    }
    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }
    public Set<Student> getStudents() {
        return students;
    }
    public void setStudents(Set<Student> students) {
        this.students = students;
    }
    @Override
    public String toString() {
        return "Teacher [id=" + id + ", teacherName=" + teacherName + "]";
    }

}

 

1、用set集合指定对多关系

2、tostring方法重写时不能包含另一个对象,否则回导致循环调用函数异常

--------------------------------------------------------

App.java

public class App {
    static SessionFactory sessionFactory=null;
    static{
        sessionFactory =new Configuration().configure().addClass(Student.class).addClass(Teacher.class).buildSessionFactory();
    }
    @Test
    public void addApp(){
        Session sess = sessionFactory.openSession();
        Transaction ts = null;
        ts = sess.beginTransaction();
        //
        Student student1=new Student();
        student1.setStudentName("studentName1");
        student1.setStudentNumber("000001"); 
        Student student2=new Student();
        student2.setStudentName("studentName2");
        student2.setStudentNumber("000002"); 
        Student student3=new Student();
        student3.setStudentName("studentName3");
        student3.setStudentNumber("000003"); 
        Student student4=new Student();
        student4.setStudentName("studentName4");
        student4.setStudentNumber("000004"); 
        //
        Teacher teacher1=new Teacher();
        teacher1.setTeacherName("wang");
        Teacher teacher2=new Teacher();
        teacher2.setTeacherName("li");
        //
        teacher1.getStudents().add(student1);
        teacher1.getStudents().add(student2);
        teacher1.getStudents().add(student3);
        teacher1.getStudents().add(student4);
        teacher2.getStudents().add(student1);
        teacher2.getStudents().add(student3);
        teacher2.getStudents().add(student4);
        //
        sess.save(teacher1);
        sess.save(teacher2);
        sess.save(student1);
        sess.save(student2);
        sess.save(student3);
        sess.save(student4);
        ts.commit();
        // TODO: handle exception
        sess.close();
    }

 

 

------------------------------------------

总结:

1、多对多关系中两个javabean对象均存在对方对象的set集合

2、都是用many-to-many表示多对多关系

 

hibernate多对多详解

标签:his   tor   todo   turn   nsa   自己的   any   com   exce   

原文地址:http://www.cnblogs.com/sovagxa/p/7440355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!