(J2EE學習筆記)hibernate的實體映射(雙向一對一)

Hibernate的實體映射的主要任務就是實現數據庫關係表與持久化類之間的映射,其關係如圖:

wKiom1byh2jw_6mCAAAgwAiXTYQ757.png

雙向映射一對一關聯關係,通過唯一外鍵方式進行一對一關聯映射,就是一個表的外鍵和另一個表的唯一主鍵對應形成一對一映射關係。

例如,以下例子,社團與社團負責人(社長),兩者之間是一對一的關聯關係:

wKioL1byi67zrXFzAAAUZCnUtWU297.png

持久化類association.java:

public class Association implements java.io.Serializable{
    private Integer id;
    private String name;
    private Integer number;
    private President president;
    public  Association {}
    //省略各屬性set/get方法
}


持久化類president.java:

public class President implements java.io.Serializable{
    private Integer id;
    private String name;
    private Integer age;
    private Integer phone;
    private String class;
    private Association association;
    public  President {}
    //省略各屬性set/get方法
}


association表與Association類的ORM映射文件Association.hbm.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="entity.Association" table="association" catalog="stu_association">
        <id name="ID" type="java.lang.Integer">
            <column name="ID" />
            <generator class="increment" />
        </id>
        <property name="namme" type="java.lang.String">
            <column name="namme" length="20" />
        </property>
        <property name="number" type="java.lang.Integer">
            <column name="number" />
        </property>
      
        <!-- 一對一的社團負責人 -->
        <many-to-one name="president"    --|屬性名字
        class="entity.President"    --|被關聯類的名字
        column="president"    --|充當外鍵的字段名
        lazy="false"     --|是否立即加載
        cascade="all"    --|主控類的所有操作,對關聯類也同樣執行
        unique="true">    --|唯一性約束,實現一對一關聯的目的
        </many-to-one>      
    </class>
</hibernate-mapping>

已經實現了Association到President的單向關聯,如果要實現雙向的關聯,還需要在President.hbm.xml中進行配置。

President.hbm.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="entity.Associationinformation" table="associationinformation" catalog="stu_association">
        <id name="ID" type="java.lang.Integer">
            <column name="ID" />
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="20" />
        </property>
        <property name="age" type="java.lang.Integer">
            <column name="age"  />
        </property>
        <property name="phone" type="java.lang.Integer">
            <column name="phone" />
        </property>   
        <property name="class" type="java.lang.String">
            <column name="class" length="20" />
        </property>
          <one-to-one name="association"    --|屬性名字
      class="entity.Association"    --|被關聯的類的名字
      property-ref="president">     --|指定關聯類的屬性名
          </one-to-one>     
    </class>
</hibernate-mapping>


測試方法Test.java:

public void add(){
//社團
    Association association = new Association();
    association.setName("社團名字");
    association.setNumber(100);
//負責人
    President president = new President();
    president.setName("張三");
    president.setAge(20);
    president.setPhone(12345678);
    president.setClass("高一(1)班");
//設置關聯
    association.setPresident(president);
    president.setAssociation(association);    
    session.save(association);
}



執行add()方法後,會向數據庫中分別插入社團和負責人的屬性,並且在社團的數據口表中,會多出一個president的字段,用於存放負責人的ID,從而實現了一對一的關聯關係。

tips:

    由於Association.hbm.xml中的cascade="all",所以當對Association進行了保存(save)操作後,與其一對一關聯的President也會執行同樣的操作


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章