hibernate面試題 --- hibernate三種檢索策略優缺點
hibernate三種檢索策略優缺點
一 檢索策略分類和介紹
hiebenate檢索策略分為類級別的檢索策略包括:立即檢索、延遲檢索。關係級別的檢索策略包括:立即檢索、延遲檢索、迫切左外連線檢索。
類級別的檢索預設的檢索策略是立即檢索。在hibernate對映檔案中,通過在上配置 lazy屬性來確定檢索策略。對於session的檢索方式,類級別檢索策略僅適用於
load方法;也就說,對於get、qurey檢索,持久化物件都會被立即載入而不管lazy是false還是true.一般來說,我們檢索物件就是要訪問它,因此立即檢索是通常的選擇。由於
load方法在檢索不到物件時會丟擲異常(立即檢索的情況下),因此我個人並不建議使用load檢索;而由於中的lazy屬性還影響到多對一及一對一的檢索策略,因此使用
load方法就更沒必要了。
關係級別的檢索策略主要體現在set集合標籤中,對檢索策略的影響取決於lazy和fecth屬性:
lazy:主要決定orders集合被初始化的時機,即到底是在載入customer物件時就被初始化,還是在程式訪問orders集合時被初始化
fetch:取值為select或subselect時,決定初始化orders的查詢語句的形式,若取值為join,則決定orders集合被初始化的時機
若把fecth設定為join,lazy屬性將被忽略
注:fetch預設值是select,lazy的預設值是true
迫切左外連線檢索(優先順序最高)
fetch 屬性有三種值
1.當用到fetch=join時一定是迫切左外連線檢索.
2.當用到fetch=select一定不採用迫切左外連線檢索,檢索方式與lazy保持一致.
3.當用到fetch=subselect,檢索多個customer時,採用巢狀的子查詢訂單.
注:fecth和lazy相比,fecth的優先順序高
執行的效果為:
假如是根據顧客去查顧客的訂單的資訊,結果如下:
(1) 沒有加fecth時的查詢結果
hibernate: select customers0_.id as id0_0_, customers0_.realname as realname0_0_, customers0_.pass as pass0_0_, customers0_.*** as ***0_0_,
customers0_.petname as petname0_0_, customers0_.email as email0_0_, customers0_.rdate as rdate0_0_ from customers customers0_ where customers0_.id=?
hibernate: select orders0_.cid as cid0_1_, orders0_.id as id1_, orders0_.id as id1_0_, orders0_.number as number1_0_, orders0_.address as address1_0_,
orders0_.phone as phone1_0_, orders0_.odate as odate1_0_, orders0_.sum as sum1_0_, orders0_.status as status1_0_, orders0_.cid as cid1_0_ from orders
orders0_ where orders0_.cid=?
可見是兩條sql查詢語句
(2)當fecth=join時,lazy=true時,查詢的語句為:
hibernate: select customers0_.id as id0_1_, customers0_.realname as realname0_1_, customers0_.pass as pass0_1_, customers0_.*** as ***0_1_,
customers0_.petname as petname0_1_, customers0_.email as email0_1_, customers0_.rdate as rdate0_1_, orders1_.cid as cid0_3_, orders1_.id as id3_, orders1_.id
as id1_0_, orders1_.number as number1_0_, orders1_.address as address1_0_, orders1_.phone as phone1_0_, orders1_.odate as odate1_0_, orders1_.sum as sum1_0_,
orders1_.status as status1_0_, orders1_.cid as cid1_0_ from customers customers0_ left outer join orders orders1_ on customers0_.id=orders1_.cid where
customers0_.id=?
根據左外連線查詢的是一條語句
(3)當fecth=join時,lazy=false時,查詢的語句為:
hibernate: select customers0_.id as id0_1_, customers0_.realname as realname0_1_, customers0_.pass as pass0_1_, customers0_.*** as ***0_1_,
customers0_.petname as petname0_1_, customers0_.email as email0_1_, customers0_.rdate as rdate0_1_, orders1_.cid as cid0_3_, orders1_.id as id3_, orders1_.id
as id1_0_, orders1_.number as number1_0_, orders1_.address as address1_0_, orders1_.phone as phone1_0_, orders1_.odate as odate1_0_, orders1_.sum as sum1_0_,
orders1_.status as status1_0_, orders1_.cid as cid1_0_ from customers customers0_ left outer join orders orders1_ on customers0_.id=orders1_.cid where
customers0_.id=?
(4)當fecth=join時,lazy=extre時,查詢的語句為:
hibernate: select customers0_.id as id0_1_, customers0_.realname as realname0_1_, customers0_.pass as pass0_1_, customers0_.*** as ***0_1_,
customers0_.petname as petname0_1_, customers0_.email as email0_1_, customers0_.rdate as rdate0_1_, orders1_.cid as cid0_3_, orders1_.id as id3_, orders1_.id
as id1_0_, orders1_.number as number1_0_, orders1_.address as address1_0_, orders1_.phone as phone1_0_, orders1_.odate as odate1_0_, orders1_.sum as sum1_0_,
orders1_.status as status1_0_, orders1_.cid as cid1_0_ from customers customers0_ left outer join orders orders1_ on customers0_.id=orders1_.cid where
customers0_.id=?
從以上的三種狀態可以看出,當fecth設定為join的時候,不管lazy是什麼值,最終的結果都是一樣的。
(4) 當fecth=select時,lazy=true時,結果為:立即檢索
(5)當fecth=select,lazy=false時,結果為:延遲檢索
(6)當fecth=select,lazy=extra時,結果為:延遲檢索
(7)當fecth=subselect,lazy也有三種情況,
巢狀子查詢(檢索多個customer物件時) lazy屬性決定檢索策略)
select customer_id,order_number,price from orders where customer_id
in (select id from customers)
二 三種檢索策略的優缺點
1.立即檢索
優點:對應用程式完全透明,不管物件處於持久化狀態還是游離狀態,應用程式都可以從乙個物件導航到關聯的物件
缺點:(1)select()語句多
(2)可能會載入應用程式不需要訪問的物件,浪費許多記憶體空間
優先考慮使用的場合
(1)類級別
(2)應用程式需要立即訪問的物件
(3)使用了二級快取
2.延遲檢索
優點:由應用程式決定需要載入哪些物件,可以避免執行多餘 的select語句,以及避免載入應用程式不需要訪問的物件。因此能提高檢索效能,並節省記憶體空間
缺點:應用程式如果希望訪問游離狀態的**類例項,必須保證它在持久化狀態時已經被初始化。
優先考慮使用的場合
(1)一對多或者多對多關聯
(2)應用程式不需要立即訪問或者根本不會訪問的物件
3.迫切左外連線檢索
優點:(1)對應用程式完全透明,不管物件處於持久化狀態還是游離狀態,都可從乙個物件導航到另乙個物件。
(2)使用了外連線,select語句少
缺點:(1)可能會載入應用程式不需要訪問的物件,浪費記憶體。
(2)複雜的資料庫表連線也會影響檢索效能。
優先考慮使用的場合
(1)多對一
(2)需要立即訪問的物件
(3)資料庫有良好的表連線效能。
Hibernate 一級快取
1.session級別的快取,同session繫結,生命週期和session相同,當session銷毀,它也清除。管理以及快取的方法有clear evict clear清除session快取,evict將某個物件清除快取,相當於成為游離態detached。2.是實體級別的快取,只有在查詢物件級別的時...
Hibernate 一級快取
1.快取 hibernate中也存在快取.hibernate中存在的快取也是用來提高效率.2.hibernate中存在兩種快取 1 執行緒級別的快取.session快取 2 程序級別的快取.hibernate 二級快取 3.session快取 就是session物件中存在的快取.快取中存在的是 持久...
hibernate 一級快取
若 號碼中已經有這個號碼,那麼將它的擴充套件碼自動增加1,以保證每次傳送的 號碼加擴充套件碼組成唯一號碼 public synchronized void updateextcode string phonenumber throws exception 專案中一段邏輯是 1 通過 手機號 字段找到...