Hibernate 關聯關係映射實例

雙向多對一/一對多(many-to-one/one-to-many)

例子,多個學生對應一個班級,一個班級對應多個學生:java

班級類,Grade.java:數據庫

public class Grade {

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

}

學生類,Student.javaapp

public class Student {

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

班級類(一的一方)映射文件,Grade.hbm.xml:spa

<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

<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)

例子,一個學生對應一個學生證,一個學生證對應一個學生:code

學生類,Student.javaxml

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

學生證類,Paper.javablog

public class Paper {

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

學生類(無外鍵一方)映射文件,Student.hbm.xml:ip

<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:get

<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創建多對多關係。

相關文章
相關標籤/搜索