日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hibernate关联关系配置(一对多、一对一和多对多)

發(fā)布時間:2023/12/3 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hibernate关联关系配置(一对多、一对一和多对多) 小編覺得挺不錯的,現(xià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ù)據(jù)庫中的多對多關聯(lián)關系一般需采用中間表的方式處理,將多對多轉(zhuǎn)化為兩個一對多。
數(shù)據(jù)表間多對多關系如下圖:

多對多關系在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>

  其實多對多就是兩個一對多,它的配置沒什么新奇的相對于一對多。在多對多的關系設計中,一般都會使用一個中間表將他們拆分成兩個一對多。標簽中的”table”屬性就是用于指定中間表的。中間表一般包含兩個表的主鍵值,該表用于存儲兩表之間的關系。由于被拆成了兩個一對多,中間表是多方,它是使用外鍵關聯(lián)的,是用于指定外鍵的,用于從中間表取出相應的數(shù)據(jù)。中間表每一行數(shù)據(jù)只包含了兩個關系表的主鍵,要獲取與自己關聯(lián)的對象集合,還需要取出由外鍵所獲得的記錄中的另一個主鍵值,由它到對應的表中取出數(shù)據(jù),填充到集合中。中的”column”屬性是用于指定按那一列的值獲取對應的數(shù)據(jù)。
  例如用course表來說,它與student表使用一個中間表student_course關聯(lián)。如果要獲取course記錄對應的學生記錄,首先需要使用外鍵”course_id”從student_course表中取得相應的數(shù)據(jù),然后在取得的數(shù)據(jù)中使用”student_id”列的值,在student表中檢索出相關的student數(shù)據(jù)。其實,為了便于理解,你可以在使用course表的使用就把中間表看成是student表,反之亦然。這樣就可以使用一對多的思維來理解了,多方關聯(lián)一方需要外鍵那么在本例子中就需要”course_id”來關。

第三種關聯(lián)關系:一對一
  一對一關系就球隊與球隊所在地之間的關系,一支球隊僅有一個地址,而一個地區(qū)也僅有一支球隊(貌似有點勉強,將就下吧)。數(shù)據(jù)表間一對一關系的表現(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),其實可以看做是一對多的一種特殊形式,多方退化成一。多方退化成一只需要在標簽中設置”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屬性的主鍵值。

總結(jié)

以上是生活随笔為你收集整理的Hibernate关联关系配置(一对多、一对一和多对多)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。