歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Hibernate關聯映射 --- 多對多實例分析(雙向關聯)

一  概念分析

1.多對多的經典實例(Teacher – Student)

二  代碼分析

1.Teacher類

package com.hbsi.domain;

import java.util.Set;

//單向一對多  Teacher 一  對  多  Student

public class Teacher {

    private int id;

    private String name;

    private Set<Student> students;

    public Teacher() {

       super();

       // TODO Auto-generated constructor stub

    }

    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;

    }

   

}

2.Stduent類

package com.hbsi.domain;

import java.util.Set;

public class Student {

    private int id;

    private String name;

    private Set<Teacher> teachers;

    public Student() {

       super();

       // TODO Auto-generated constructor stub

    }

    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.Teacher的映射文件  Teacher.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.hbsi.domain">

    <class name="Teacher" table="teacher">

       <id name="id" column="id">

           <generator class="native" />

       </id>

       <property name="name" column="name" />

       <!-- 集合屬性的體現  多對多  table屬性指定的是中間表的名字 -->

       <set name="students" table="teacher_student">

           <!--查中間表的外鍵 和老師的id的關聯    老師的id-->

           <key column="teacher_id"/>

           <!-- 查學生的id和中間表的關系 -->

           <many-to-many class="Student" column="student_id"></many-to-many>

       </set>

    </class>

</hibernate-mapping>

4.Student的映射文件  Student.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.hbsi.domain">

    <class name="Student" table="student">

       <id name="id" column="id">

           <generator class="native" />

        </id>

       <property name="name" column="name" />

       <set name="teachers" table="teacher_student">

           <key column="student_id"></key><!-- 查找這個學生被那個老師教 -->

           <many-to-many class="Teacher" column="teacher_id"></many-to-many>

       </set>

    </class>

</hibernate-mapping>

Copyright © Linux教程網 All Rights Reserved