Hibernate一對多 雙向

2021-06-01 22:15:41 字數 882 閱讀 4272

///

hibernate:

///雙向關聯就是有「一對多」和「多對一」兩個關聯組合而成德,在雙向關聯的兩端都知道對方是誰。

下面就開始演示這種關聯。

首先定義我們需要使用的pojo物件。

public class member

public class order

兩個pojo對應的對映檔案分別為member.hbm.xml和order.hbm.xml。

member.hbm.xml

order.hbm.xml

下面我們來進行測試。

a、插入資料

public void insert()

檢視hibernate在後台使用的sql語句如下:

hibernate:

insert

into

test_member

(age, name, id)

values

(?, ?, ?)

hibernate:

insert

into

test_order

(name, num, member_id)

values

(?, ?, ?)

僅僅就兩條語句,明顯比單向關聯的時候少了一條更新語句(把order的外來鍵進行更新為menber的主鍵值)。

b、載入資料

和單向關聯的區別在於,如果載入的是order物件,那麼它也知道和它關聯的物件。

c、刪除資料

刪除資料和單向關聯時的刪除資料的區別在於,對member及它的關聯物件order載入後,不需要在更新order的外來鍵為null,而是緊跟著逐條刪除order,最後刪除member。

hibernate單向一對多和雙向一對多

單向一對多 例如有部門封裝類 private int deptno private string deptname private string location 有職員封裝類 private int empno private string empname private dept dept 在多...

雙向一對多

雙向一對多是many to one的雙方都有用註解來維持關聯關係 many的一方 joincolumn name d id manytoone fetch fetchtype.lazy private dept dept one的一方 一對多關聯 joincolumn name d id oneto...

Hibernate(九)一對多雙向關聯對映

一對多雙向關聯對映 一對多雙向關聯映 射,即在一的一端存在多的一端的乙個集合物件,在多的一端存在一的一端的乙個物件,這樣就可以保證在加 載一的一端或多的一端將被指向端的集合或物件載入上來,即保證雙向關聯。一對多雙向關聯對映和 一對多單向關聯對映的異同 一對多雙向關聯對映相應的類結構圖和 具體如下 p...