hibernate中get 與 load 區別

2021-08-25 00:15:29 字數 3174 閱讀 3302

session.get 與 session.load區別

1. 對於get方法,hibernate會確認一下該id對應的資料是否存在,首先在session快取中查詢,然後在二級快取中查詢,還沒有就查詢資料庫,資料庫中沒有就返回null。

2. load方法載入實體物件的時候,根據對映檔案上類級別的lazy屬性的配置(預設為true),分情況討論:

(1)若為true,則首先在session快取中查詢,看看該id對應的物件是否 存在,不存在則使用延遲載入,返回實體的**類物件(該**類為實體類的子類,由cglib動態生成)。等到具體使用該物件(除獲取oid以外)的時候, 再查詢二級快取和資料庫,若仍沒發現符合條件的記錄,則會丟擲乙個objectnotfoundexception。

(2)若為false,就跟get方法查詢順序一樣,只是最終若沒發現符合條件的記錄,則會丟擲乙個objectnotfoundexception。

這裡get和load有兩個重要區別:

如果未能發現符合條件的記錄,get方法返回null,而load方法會丟擲乙個objectnotfoundexception。

load方法可返回沒有載入實體資料的**類例項,而get方法永遠返回有實體資料的物件。(對於load和get方法返回型別:好多書中都說:「get方法永遠只返回實體類」,實際上並不正確,get方法如果在session快取中找到了該id對應的物件,如果剛好該物件前面是被**過的,如被load方法使用過,或者被其他關聯物件延遲載入過,那麼返回的還是原先的**物件,而不是實體類物件,如果該**物件還沒有載入實體資料(就是id以外的其他屬性資料),那麼它會查詢二級快取或者資料庫來載入資料,但是返回的還是**物件,只不過已經載入了實體資料。)

以下是**例項:

category.hbm.xml:

lazy="true" ><<<<<<<<<<<<<<<<2.

daoimpl:

public category get(int id)

public t load(int id)

1.情況如下時

junit:

private categorydao categorydao = new categorydaoimpl();

public void testget()

a. 1.lazy="false" 2.lazy="true" 時:

console:

hibernate: select category0_.id_ as id1_0_0_, category0_.name_ as name2_0_0_, category0_.desc_ as desc3_0_0_, category0_.order_ as order4_0_0_ from itcast_category category0_ where category0_.id_=?

開啟注釋3時

console:

hibernate: select category0_.id_ as id1_0_0_, category0_.name_ as name2_0_0_, category0_.desc_ as desc3_0_0_, category0_.order_ as order4_0_0_ from itcast_category category0_ where category0_.id_=?

2, b

開啟注釋4時

console:

hibernate: select category0_.id_ as id1_0_0_, category0_.name_ as name2_0_0_, category0_.desc_ as desc3_0_0_, category0_.order_ as order4_0_0_ from itcast_category category0_ where category0_.id_=?

2, b

hibernate: select forums0_.category_ as category5_1_, forums0_.id_ as id1_1_, forums0_.id_ as id1_1_0_, forums0_.name_ as name2_1_0_, forums0_.desc_ as desc3_1_0_, forums0_.order_ as order4_1_0_, forums0_.category_ as category5_1_0_ from itcast_forum forums0_ where forums0_.category_=? order by forums0_.order_ asc

[id=2,name=b1,order=2, id=3,name=b2,order=3]

b. 1.lazy="true" 2.lazy="true" 時:

console:

開啟注釋3時

console:

hibernate: select category0_.id_ as id1_0_0_, category0_.name_ as name2_0_0_, category0_.desc_ as desc3_0_0_, category0_.order_ as order4_0_0_ from itcast_category category0_ where category0_.id_=?

2, b

開啟注釋4時

console:

hibernate: select category0_.id_ as id1_0_0_, category0_.name_ as name2_0_0_, category0_.desc_ as desc3_0_0_, category0_.order_ as order4_0_0_ from itcast_category category0_ where category0_.id_=?

2, b

hibernate: select forums0_.category_ as category5_1_, forums0_.id_ as id1_1_, forums0_.id_ as id1_1_0_, forums0_.name_ as name2_1_0_, forums0_.desc_ as desc3_1_0_, forums0_.order_ as order4_1_0_, forums0_.category_ as category5_1_0_ from itcast_forum forums0_ where forums0_.category_=? order by forums0_.order_ asc

[id=2,name=b1,order=2, id=3,name=b2,order=3]

關於Hibernate中load與get的討論

路人甲 hibernate中有兩個極為相似的方法get 與load 他們都可以通過指定的實體類與id從資料庫中讀取資料,並返回對應的例項,但hibernate不會搞兩個完全一樣的方法的,它們間的不同在於 如果找不到符合條件的紀錄,get 方法將返回null 而load 將會報出objectnotfo...

Hibernate 中get與load的區別

hibernate中用於獲取資料的方法被有意識的分成2種分類 1.立刻載入方法 get list 2.延遲載入方法 load iterate select id from 表 where 條件 n a 所謂的 立刻載入 就是首先在session快取中查詢,然後在二級快取中查詢,還沒有就查詢資料庫,數...

Hibernate中的get與load有什麼區別

學校學習版 1 get是立即載入,load是延遲載入 2 查詢不到資料的時候,get返回null,load拋異常 3 get預設立即載入 返回非 物件 load預設是延遲載入 返回 物件 4 在hibernate實體對映檔案中可修改lazy屬性的值來改變load方法的載入方式 lazy false ...