Hibernate關聯(lián)關系配置
第一種關聯(lián)關系:一對多(多對一)
"一對多"是最普遍的映射關系,簡單來講就如消費者與訂單的關系。
一對多:從消費者角的度來說一個消費者可以有多個訂單,即為一對多。
多對一:從訂單的角度來說多個訂單可以對應一個消費者,即為多對一。
一對多關系在hbm文件中的配置信息:
消費者(一方):
- <?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">
- <hibernate-mapping>
- <class name="com.suxiaolei.hibernate.pojos.Customer" table="customer">
- <!-- 主鍵設置 -->
- <id name="id" type="string">
- <column name="id"></column>
- <generator class="uuid"></generator>
- </id>
- <!-- 屬性設置 -->
- <property name="username" column="username" type="string"></property>
- <property name="balance" column="balance" type="integer"></property>
- <set name="orders" inverse="true" cascade="all">
- <key column="customer_id" ></key>
- <one-to-many class="com.suxiaolei.hibernate.pojos.Order"/>
- </set>
- </class>
- </hibernate-mapping>
訂單(多方):
- <?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">
- <hibernate-mapping>
- <class name="com.suxiaolei.hibernate.pojos.Order" table="orders">
- <id name="id" type="string">
- <column name="id"></column>
- <generator class="uuid"></generator>
- </id>
- <property name="orderNumber" column="orderNumber" type="string"></property>
- <property name="cost" column="cost" type="integer"></property>
- <many-to-one name="customer" class="com.suxiaolei.hibernate.pojos.Customer"
- column="customer_id" cascade="save-update">
- </many-to-one>
- </class>
- </hibernate-mapping>
"一對多"關聯(lián)關系,Customer方對應多個Order方,所以Customer包含一個集合用于存儲多個Order,Order包含一個Customer用于儲存關聯(lián)自己的Customer。
一對多關聯(lián)關系有一種特例:自身一對多關聯(lián)。例如:
自身一對多關聯(lián)自身的hbm文件設置:
- <?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">
- <hibernate-mapping>
- <class name="com.suxiaolei.hibernate.pojos.Category" table="category">
- <id name="id" type="string">
- <column name="id"></column>
- <generator class="uuid"></generator>
- </id>
- <property name="name" column="name" type="string"></property>
- <set name="chidrenCategories" cascade="all" inverse="true">
- <key column="category_id"></key>
- <one-to-many class="com.suxiaolei.hibernate.pojos.Category"/>
- </set>
- <many-to-one name="parentCategory" class="com.suxiaolei.hibernate.pojos.Category" column="category_id">
- </many-to-one>
- </class>
- </hibernate-mapping>
外鍵存放父親的主鍵。
第二種關聯(lián)關系:多對多
多對多關系也很常見,例如學生與選修課之間的關系,一個學生可以選擇多門選修課,而每個選修課又可以被多名學生選擇。數(shù)據庫中的多對多關聯(lián)關系一般需采用中間表的方式處理,將多對多轉化為兩個一對多。
數(shù)據表間多對多關系如下圖:
多對多關系在hbm文件中的配置信息:
學生:
- <?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">
- <hibernate-mapping>
- <class name="com.suxiaolei.hibernate.pojos.Student" table="student">
- <id name="id" type="integer">
- <column name="id"></column>
- <generator class="increment"></generator>
- </id>
- <property name="name" column="name" type="string"></property>
- <set name="courses" inverse="false" cascade="save-update" table="student_course">
- <key column="student_id"></key>
- <many-to-many class="com.suxiaolei.hibernate.pojos.Course"
- column="course_id"></many-to-many>
- </set>
- </class>
- </hibernate-mapping>
課程:
- <?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">
- <hibernate-mapping>
- <class name="com.suxiaolei.hibernate.pojos.Course" table="course">
- <id name="id" type="integer">
- <column name="id"></column>
- <generator class="increment"></generator>
- </id>
- <property name="name" column="name" type="string"></property>
- <set name="students" inverse="true" cascade="save-update" table="student_course">
- <key column="course_id"></key>
- <many-to-many class="com.suxiaolei.hibernate.pojos.Student"
- column="student_id"></many-to-many>
- </set>
- </class>
- </hibernate-mapping>
其實多對多就是兩個一對多,它的配置沒什么新奇的相對于一對多。在多對多的關系設計中,一般都會使用一個中間表將他們拆分成兩個一對多。<set>標簽中的"table"屬性就是用于指定中間表的。中間表一般包含兩個表的主鍵值,該表用于存儲兩表之間的關系。由于被拆成了兩個一對多,中間表是多方,它是使用外鍵關聯(lián)的,<key>是用于指定外鍵的,用于從中間表取出相應的數(shù)據。中間表每一行數(shù)據只包含了兩個關系表的主鍵,要獲取與自己關聯(lián)的對象集合,還需要取出由外鍵所獲得的記錄中的另一個主鍵值,由它到對應的表中取出數(shù)據,填充到集合中。<many-to-many>中的"column"屬性是用于指定按那一列的值獲取對應的數(shù)據。
例如用course表來說,它與student表使用一個中間表student_course關聯(lián)。如果要獲取course記錄對應的學生記錄,首先需要使用外鍵"course_id"從student_course表中取得相應的數(shù)據,然后在取得的數(shù)據中使用"student_id"列的值,在student表中檢索出相關的student數(shù)據。其實,為了便于理解,你可以在使用course表的使用就把中間表看成是student表,反之亦然。這樣就可以使用一對多的思維來理解了,多方關聯(lián)一方需要外鍵那么在本例子中就需要"course_id"來關。
第三種關聯(lián)關系:一對一
一對一關系就球隊與球隊所在地之間的關系,一支球隊僅有一個地址,而一個地區(qū)也僅有一支球隊(貌似有點勉強,將就下吧)。數(shù)據表間一對一關系的表現(xiàn)有兩種,一種是外鍵關聯(lián),一種是主鍵關聯(lián)。圖示如下:
一對一外鍵關聯(lián):
一對一主鍵關聯(lián):要求兩個表的主鍵必須完全一致,通過兩個表的主鍵建立關聯(lián)關系:
一對一外鍵關聯(lián)在hbm文件中的配置信息:
地址:
- <?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">
- <hibernate-mapping>
- <class name="com.suxiaolei.hibernate.pojos.Adress" table="adress">
- <id name="id" type="integer">
- <column name="id"></column>
- <generator class="increment"></generator>
- </id>
- <property name="city" column="city" type="string"></property>
- <one-to-one name="team" class="com.suxiaolei.hibernate.pojos.Team" cascade="all"></one-to-one>
- </class>
- </hibernate-mapping>
球隊:
- <?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">
- <hibernate-mapping>
- <class name="com.suxiaolei.hibernate.pojos.Team" table="team">
- <id name="id" type="integer">
- <column name="id"></column>
- <generator class="increment"></generator>
- </id>
- <property name="name" column="name" type="string"></property>
- <many-to-one name="adress" class="com.suxiaolei.hibernate.pojos.Adress" column="adress_id" unique="true"></many-to-one>
- </class>
- </hibernate-mapping>
一對一外鍵關聯(lián),其實可以看做是一對多的一種特殊形式,多方退化成一。多方退化成一只需要在<many-to-one>標簽中設置"unique"="true"。
一對一主鍵關聯(lián)在hbm文件中的配置信息:
地址:
- <hibernate-mapping>
- <class name="com.suxiaolei.hibernate.pojos.Adress" table="adress">
- <id name="id" type="integer">
- <column name="id"></column>
- <generator class="increment"></generator>
- </id>
- <property name="city" column="city" type="string"></property>
- <one-to-one name="team" class="com.suxiaolei.hibernate.pojos.Team" cascade="all"></one-to-one>
- </class>
- </hibernate-mapping>
球隊:
- <hibernate-mapping>
- <class name="com.suxiaolei.hibernate.pojos.Team" table="team">
- <id name="id" type="integer">
- <column name="id"></column>
- <generator class="foreign">
- <param name="property">adress</param>
- </generator>
- </id>
- <property name="name" column="name" type="string"></property>
- <one-to-one name="adress" class="com.suxiaolei.hibernate.pojos.Adress" cascade="all"></one-to-one>
- </class>
- </hibernate-mapping>
一對一主鍵關聯(lián),是讓兩張的主鍵值一樣。要使兩表的主鍵相同,只能一張表生成主鍵,另一張表參考主鍵。
- <generator class="foreign">
- <param name="property">adress</param>
- </generator>
"class"="foreign"就是設置team表的主鍵參照adress屬性的主鍵值。
原文鏈接:http://www.cnblogs.com/otomedaybreak/archive/2012/01/20/2327695.html
【編輯推薦】