Hibernate 关联关系映射实例

双向多对一/一对多(many-to-one/one-to-many)

例子,多个学生对应一个班级,一个班级对应多个学生:

班级类,Grade.java:

public class Grade {

    private Integer id;
    private String name;
    private String description;
    //学生集合
    private Set<Student> students = new HashSet<Student>();
    
    //省略get,set方法......

}

学生类,Student.java

public class Student {

    private Integer id;
    private String name;
    private String sex;
    //班级引用
    private Grade grade;
    
    //省略get,set方法.......
    
}

班级类(一的一方)映射文件,Grade.hbm.xml:

<hibernate-mapping package="accp.hib">
    <class name="Grade" table="Grade" >
        <id name="id" column="id">
            <generator class="native" />
        </id>
        <property name="name" column="name" type="string" />
        <property name="description" type="string" column="description" />
        <!-- 映射学生类集合 -->
        <set name="students" inverse="true">
            <key column="grade_id" />
            <one-to-many class="Student"/>
        </set>
    </class>
</hibernate-mapping>

学生类(多的一方)映射文件,Student.hbm.xml:

<hibernate-mapping package="accp.hib">
    <class name="Student" table="Student">
        <id name="id" column="id">
            <generator class="native" />
        </id>
        <property name="name" column="name" type="string" />
        <property name="sex" type="string" column="sex" />
        <!-- 映射班级类引用 -->
        <many-to-one name="grade" class="Grade" column="grade_id" cascade="save-update"/>
    </class>
</hibernate-mapping>

 

双向一对一(One-to-one)

例子,一个学生对应一个学生证,一个学生证对应一个学生:

学生类,Student.java

public class Student {
    private Integer id;
    private String name;
    private String sex;
    //学生证类 引用
    private Paper paper;
    
    //省略get,set方法......
}

学生证类,Paper.java

public class Paper {

    private Integer id;
    private String description;
    //学生类引用
    private Student student;
    
    //省略get,set方法.....  
}

学生类(无外键一方)映射文件,Student.hbm.xml:

<hibernate-mapping package="accp.hib">
    <class name="Student" table="Student">
        <id name="id" column="id">
            <generator class="native" />
        </id>
        <property name="name" column="name" type="string" />
        <property name="sex" type="string" column="sex" />
        <!-- 映射学生证类 -->
        <one-to-one name="paper" class="Paper" />
    </class>
</hibernate-mapping>

学生证类(有外键一方)映射文件,Paper.hbm.xml:

<hibernate-mapping package="accp.hib">
    <class name="Paper" table="Paper" >
        <id name="id" column="id">
            <generator class="native" />
        </id>
        <property name="description" type="string" column="description" />
        <!-- 映射学生类 -->
        <many-to-one name="student" column="student_id" class="Student" unique="true" cascade="save-update"/>
    </class>
</hibernate-mapping>

要数据库中,外键会建立在写有 many-to-one 所映射的表中,也就是paper类所映射的表。

 

双向多对多(many-to-many)

例子,多个学生对应多门课程,多门课程也对应多个学生:

学生类,Student.java

public class Student {
    private Integer id;
    private String name;
    private String sex;
    //课程类集合
    private Set<Course> courses = new HashSet<Course>();
    
    //省略get,set方法.......
}

课程类,Course.java

public class Course {
    private Integer id;
    private String name;
    //学生类集合
    private Set<Student> students = new HashSet<Student>();
    
    //省略get,set方法.......
}

学生类映射文件,Student.hbm.xml:

<hibernate-mapping package="accp.hib">
    <class name="Student" table="Student">
        <id name="id" column="id">
            <generator class="native" />
        </id>
        <property name="name" column="name" type="string" />
        <property name="sex" type="string" column="sex" />
        <!-- 映射课程类集合 -->
        <set name="courses" table="sc">
            <key column="student_id" />
            <many-to-many column="course_id" class="Course"/>
        </set>
    </class>
</hibernate-mapping>

课程类映射文件,Course.hbm.xml:

<hibernate-mapping package="accp.hib">
    <class name="Course" table="Course" >
        <id name="id" column="id">
            <generator class="native" />
        </id>
        <property name="name" column="name" type="string" />
        <!-- 映射学生类集合 -->
        <set name="students" table="sc" inverse="true">
            <key column="course_id" />
            <many-to-many column="student_id" class="Student" />
        </set>
    </class>
</hibernate-mapping>

会新建一张名为 sc 的中间表,该表拥有student和course的外键,通过该中间表为student和course建立多对多关系。

posted on 2013-11-16 19:48  喜欢凯的蓝  阅读(1204)  评论(0编辑  收藏  举报