Hibernate關聯關係配置(一對一,一對多,多對多)
第一種關聯關係:一對多(多對一)
"一對多"是最普遍的對映關係,簡單來講就如消費者與訂單的關係。
一對多:從消費者角的度來說一個消費者可以有多個訂單,即為一對多。
多對一:從訂單的角度來說多個訂單可以對應一個消費者,即為多對一。
一對多關係在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>
"一對多"關聯關係,Customer方對應多個Order方,所以Customer包含一個集合用於儲存多個Order,Order包含一個Customer用於儲存關聯自己的Customer。
一對多關聯關係有一種特例:自身一對多關聯。例如:
自身一對多關聯自身的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>
外來鍵存放父親的主鍵。
第二種關聯關係:多對多
多對多關係也很常見,例如學生與選修課之間的關係,一個學生可以選擇多門選修課,而每個選修課又可以被多名學生選擇。資料庫中的多對多關聯關係一般需採用中間表的方式處理,將多對多轉化為兩個一對多。
資料表間多對多關係如下圖:
多對多關係在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"屬性就是用於指定中間表的。中間表一般包含兩個表的主鍵值,該表用於儲存兩表之間的關係。由於被拆成了兩個一對多,中間表是多方,它是使用外來鍵關聯的,<key>是用於指定外來鍵的,用於從中間表取出相應的資料。中間表每一行資料只包含了兩個關係表的主鍵,要獲取與自己關聯的物件集合,還需要取出由外來鍵所獲得的記錄中的另一個主鍵值,由它到對應的表中取出資料,填充到集合中。<many-to-many>中的"column"屬性是用於指定按那一列的值獲取對應的資料。
例如用course表來說,它與student表使用一箇中間表student_course關聯。如果要獲取course記錄對應的學生記錄,首先需要使用外來鍵"course_id"從student_course表中取得相應的資料,然後在取得的資料中使用"student_id"列的值,在student表中檢索出相關的student資料。其實,為了便於理解,你可以在使用course表的使用就把中間表看成是student表,反之亦然。這樣就可以使用一對多的思維來理解了,多方關聯一方需要外來鍵那麼在本例子中就需要"course_id"來關。
第三種關聯關係:一對一
一對一關係就球隊與球隊所在地之間的關係,一支球隊僅有一個地址,而一個地區也僅有一支球隊(貌似有點勉強,將就下吧)。資料表間一對一關係的表現有兩種,一種是外來鍵關聯,一種是主鍵關聯。圖示如下:
一對一外來鍵關聯:
一對一主鍵關聯:要求兩個表的主鍵必須完全一致,通過兩個表的主鍵建立關聯關係:
一對一外來鍵關聯在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>
一對一外來鍵關聯,其實可以看做是一對多的一種特殊形式,多方退化成一。多方退化成一隻需要在<many-to-one>標籤中設定"unique"="true"。
一對一主鍵關聯在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關聯關係配置(一對一,一對多,多對多)
第一種關聯關係:一對多(多對一) "一對多"是最普遍的對映關係,簡單來講就如消費者與訂單的關係。 一對多:從消費者角的度來說一個消費者可以有多個訂單,即為一對多。 多對一:從訂單的角度來說多個
hibernate 關聯關係配置(一對多,多對一)
Hibernate 關聯關係: 1. 什麼是關聯(association) 1.1 關聯指的是類之間的引用關係。如果類A與類B關聯,那麼被引用的類B將被定義為類
Hibernate關聯關係配置-----基於連線表的雙向多對多配置
實體:package bi.many2many.jointable; import java.util.HashSet; import java.util.Set; public class Student { private int id; private Stri
hibernate關聯關係註解(一對多、多對一、一對一)
一對多、多對一、一對一的註解實現 casecade={CasecadeType.REFRESH}的含義是隻是查詢級聯,它還有其他3個型別: CascadeType.MERGE級聯更新 CascadeType.PERSIST級聯儲存 CascadeType.REMOVE級聯刪
Hibernate關聯關係對映-----基於連線表的單向多對多對映配置
實體:package uni.many2many.jointable; import java.util.HashSet; import java.util.Set; public class Student { private int id; private Str
Hibernate關聯關係配置-----基於連線表的雙向一對一對映配置
實體:package bi.one2one.jointable; public class Husband { private int id; private String name; private Wife wife; public Husband() {
Hibernate關聯關係之一對一
Hibernate關聯關係之一對一 1.一對一關聯 1.1.單向一對一外來鍵關聯 實體類中屬性,因為是單向一對一,從Person到IdCard,所以Person中多一個能存放IdCard例項物件的屬性。 IdCard.java和IdCard.hbm.xml public class I
Hibernate關聯關係之一對一關聯關係的CRUD操作
說明:本文註釋的不是很多,只是功能上能簡單的基本達到。要看詳細的說明可參考另一篇博文超詳細的Hibernate關聯關係之雙向的一對多關聯關係的CRUD操作————學習至黑馬程式設計師視訊教程。。當然,one-to-one , many-to-one , many-to-man
web開發(十一)之Hibernate關聯關係配置
寫在前面 Hibernate中關係的對映共有以下四種:一對多、多對一、一對一、多對多這四種。 一對多單向關聯 xml對映 一對多即在A表中的每一條資料都會與B表中的n條有關聯;在這種情況下一般都是在B表新增一個欄位用來當作外來鍵與A表中的主鍵相關聯。而這種
hibernate表關係配置——一對多
如:訂單表與訂單項表 訂單表對應的實體類 (與訂單項表為一對多的關係) private Integer orderId; private String orderNo; /* * 在描述關係的時候,一定是集合介面進行接受 * 對應的訂單詳情一對多關係 *
hibernate關聯關係一對多
1. 什麼是關聯(association) 1.1 關聯指的是類之間的引用關係。如果類A與類B關聯,那麼被引用的類B將被定義為類A的屬性。例如: public class A{ priva
hibernate關聯關係(一對多、多對一)
1.什麼是關聯關係? 關聯關係是指兩個實體或者表有相互關聯,比如在學生表中,學生表有班級id,那麼學生表中的班級id就關聯班級表中班級Id欄位。因此關聯關係,可以看成是一種外來鍵關係。 2.為什麼要有關聯關係? 為了實現級聯操作,刪除班級中某個學生,會連帶刪除學生表中的
MyBatis學習筆記之二--關聯關係(一對一和一對多)
首先給大家推薦幾個網頁: http://blog.csdn.net/isea533/article/category/2092001 沒事看看 - MyBatis工具:www.mybatis.tk http://www.mybatis.org/mybatis-3/zh/gettin
Hibernate關聯關係對映-----基於連線表的單向一對多對映
實體:package uni.many2one.jointable; public class Parent { private int id; private String name; private Child child; public Parent() {
Hibernate關聯關係之多對多關聯關係的CRUD操作
========================================================== ========================================================== 1.專案結構: =====
Hibernate關聯關係對映-----單向一對一對映配置
這裡舉了一夫一妻的例子。實體:package uni.one2one; public class Husband { private int id; private String name; private Wife wife; public Husba
Hibernate 關聯關係對映配置
Hibernate 關聯關係對映配置 問題背景 一個部門下有多個員工,每個員工只屬於一個部門 員工對部門(多對一) 部門對員工(一對多) 配置關聯關係 實體類 Employee.java publ
Hibernate關聯關係註解配置簡單理解
Hibernate關聯關係註解配置 什麼是關聯關係?關聯關係有哪幾種? 關聯關係指實體之間的關係,也就是表與表之間的關係。一個關係用兩個屬性來描述,數量性和方向性。 從數量上來看,表與表之間主要有三種關係,一對一,一對多,多對多。 加上關係的方向,還有一個多對一。 hibernate中關聯關係的維護
hibernate關聯關係中的一對一以及懶載入的原理:lazy和fetch的理解
******************** Person.java主對像 ******************** package blog.hibernate.domain; public class Person { private int id; p
[Hibernate]七種關聯關係配置檔案和測試例項詳解
用了一天整理下來。所有關係分為以下七種:單向【1-1】雙向【1-1】單向【1-N】雙向【1-N】單向【N-1】單向【N-N】雙向【N-N】1單向【1-1】基於外來鍵的單向【1-1】是【N-1】的特殊形式,要求【N】方唯一。基於外來鍵的單向1-1只需要在原有的<many-