jpa多對一對映

2022-03-27 15:35:01 字數 3500 閱讀 2704

1.插入

建乙個部門類dept和乙個員工類emp;

emp對dept是多對一的關係;因為乙個部門有多個員工,而乙個員工只有乙個部門;

emp類中新增乙個dept的屬性;

@manytoone註解表示了員工和部門是多對一的關係;

@joincolumn註解的name屬性表示外鍵名;emp表中會多出乙個外來鍵列;

//多對一,乙個員工對應乙個部門;而乙個部門可對應多個員工;

@joincolumn(name="d_id")

@manytoone

private dept dept;

插入測試:

//測試多對一新增

@test

public void testupdate()

結果:hibernate:

insert

into

tb_dept

(dname)

values

(?)hibernate:

insert

into

tb_emp

(birthday, d_id, name, salary)

values

(?, ?, ?, ?)

hibernate:

insert

into

tb_emp

(birthday, d_id, name, salary)

values

(?, ?, ?, ?)

可以看出,先插入dept也就是少的一方,執行三條insert語句;

先插入emp,後插入dept結果:

hibernate:

insert

into

tb_emp

(birthday, d_id, name, salary)

values

(?, ?, ?, ?)

hibernate:

insert

into

tb_emp

(birthday, d_id, name, salary)

values

(?, ?, ?, ?)

hibernate:

insert

into

tb_dept

(dname)

values

(?)hibernate:

update

tb_emp

setbirthday=?,

d_id=?,

name=?,

salary=?

where

id=?

hibernate:

update

tb_emp

setbirthday=?,

d_id=?,

name=?,

salary=?

where

id=?

可以看出,先執行了三條插入語句,後執行了兩條update語句;

因為emp先插入時由於dept還沒插入,所以沒有dept的did;也就是外來鍵;

等dept插入後有了外來鍵;

為了維持兩張表的關聯關係,執行了更新語句;給兩個emp新增外來鍵;

為了提高效率,處理多對一的儲存操作時,最好先儲存少的一方;

2.查詢

@test

public void testselect()

結果:hibernate:

select

emp0_.id as id1_1_1_,

emp0_.birthday as birthday2_1_1_,

emp0_.d_id as d_id5_1_1_,

emp0_.name as name3_1_1_,

emp0_.salary as salary4_1_1_,

dept1_.did as did1_0_0_,

dept1_.dname as dname2_0_0_

from

tb_emp emp0_

left outer join

tb_dept dept1_

on emp0_.d_id=dept1_.did

where

emp0_.id=?

可以看出使用左外連線來獲取關聯物件;

1)懶載入查詢

可在@manytoone註解後面將fetch屬性改為lazy來使用懶載入;

懶載入會在需要用到dept的屬性時才執行查詢dept的sql語句;可以節省資源;

//多對一,乙個員工對應乙個部門;而乙個部門可對應多個員工;

@joincolumn(name="d_id")

@manytoone(fetch=fetchtype.lazy)

private dept dept;

結果:hibernate:

select

emp0_.id as id1_1_0_,

emp0_.birthday as birthday2_1_0_,

emp0_.d_id as d_id5_1_0_,

emp0_.name as name3_1_0_,

emp0_.salary as salary4_1_0_

from

tb_emp emp0_

where

emp0_.id=?

可以看出沒有查詢外來鍵;

3.刪除

因為有外來鍵關聯;在刪除多對一,少的一方時會報錯;

例如當dept的一條記錄還被emp中的記錄關聯時,就無法刪除,會報錯;

4.更新

在many to one 對映中,可以通過one的一方來修改many;

//測試修改

@test

public void testupdate()

結果:hibernate:

select

emp0_.id as id1_1_0_,

emp0_.birthday as birthday2_1_0_,

emp0_.d_id as d_id5_1_0_,

emp0_.name as name3_1_0_,

emp0_.salary as salary4_1_0_

from

tb_emp emp0_

where

emp0_.id=?

hibernate:

select

dept0_.did as did1_0_0_,

dept0_.dname as dname2_0_0_

from

tb_dept dept0_

where

dept0_.did=?

hibernate:

update

tb_dept

setdname=?

where

did=?

可以看出執行了兩條查詢語句和一條更新語句;

JPA單向多對多實體對映

知識點梳理 a 註解配置時使用 jointable配置中間表,joincolumns配置當前類對應的主鍵id,inversejoincolumn反向配置對應的表主鍵id b 建表時,中間表配置雙主鍵 雙外來鍵 單向多對多 create table sys role id varchar 64 pri...

JPA 對映雙向多對多關聯關係

不維護關聯關係的一端 entity table name item public class item public void setid integer id public string getitemname public void setitemname string itemname pub...

JPA系列六 對映關聯關係 單向多對一

1 建立實體類order table name jpa orders entity public class order public void setid integer id column name order name public string getordername public voi...