自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Hibernate批量更新和批量刪除實戰(zhàn)

開發(fā) 后端
Hibernate批量更新是指在一個事務(wù)中更新大批量數(shù)據(jù),Hibernate批量刪除是指在一個事務(wù)中刪除大批量數(shù)據(jù)。以下程序直接通過Hibernate API批量更新CUSTOMERS表中年齡大于零的所有記錄的AGE字段:

本文向大家介紹Hibernate批量更新和Hibernate批量刪除,可能好多人還不了解Hibernate批量更新和Hibernate批量刪除,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會你更多東西。

Hibernate批量更新是指在一個事務(wù)中更新大批量數(shù)據(jù),Hibernate批量刪除是指在一個事務(wù)中刪除大批量數(shù)據(jù)。以下程序直接通過Hibernate API批量更新CUSTOMERS表中年齡大于零的所有記錄的AGE字段:

  1. tx = session.beginTransaction();    
  2. ??Iterator customers=session.find("from Customer c where c.age>0").iterator();    
  3. ??while(customers.hasNext()){    
  4. ??Customer customer=(Customer)customers.next();    
  5. ??customer.setAge(customer.getAge()+1);    
  6. ??}     
  7. ??tx.commit();    
  8. ??session.close();  

如果CUSTOMERS表中有1萬條年齡大于零的記錄,那么Session的find()方法會一下子加載1萬個Customer對象到內(nèi)存。當(dāng)執(zhí)行tx.commit()方法時,會清理緩存,Hibernate執(zhí)行1萬條更新CUSTOMERS表的update語句:

  1. update CUSTOMERS set AGE=? …. where ID=i;    
  2. ??update CUSTOMERS set AGE=? …. where ID=j;    
  3. ??update CUSTOMERS set AGE=? …. where ID=k;  

以上Hibernate批量更新方式有兩個缺點:

(1) 占用大量內(nèi)存,必須把1萬個Customer對象先加載到內(nèi)存,然后一一更新它們。
(2) 執(zhí)行的update語句的數(shù)目太多,每個update語句只能更新一個Customer對象,必須通過1萬條update語句才能更新一萬個Customer對象,頻繁的訪問數(shù)據(jù)庫,會大大降低應(yīng)用的性能。

為了迅速釋放1萬個Customer對象占用的內(nèi)存,可以在更新每個Customer對象后,就調(diào)用Session的evict()方法立即釋放它的內(nèi)存:

  1. tx = session.beginTransaction();    
  2. Iterator customers=session.find("from Customer c where c.age>0").iterator();    
  3. while(customers.hasNext()){    
  4. Customer customer=(Customer)customers.next();    
  5. customer.setAge(customer.getAge()+1);    
  6. session.flush();    
  7. session.evict(customer);    
  8. }     
  9. tx.commit();    
  10. session.close();  

在以上程序中,修改了一個Customer對象的age屬性后,就立即調(diào)用Session的flush()方法和evict()方法,flush()方法使Hibernate立刻根據(jù)這個Customer對象的狀態(tài)變化同步更新數(shù)據(jù)庫,從而立即執(zhí)行相關(guān)的update語句;evict()方法用于把這個Customer對象從緩存中清除出去,從而及時釋放它占用的內(nèi)存。

但evict()方法只能稍微提高批量操作的性能,因為不管有沒有使用evict()方法,Hibernate都必須執(zhí)行1萬條update語句,才能更新1萬個Customer對象,這是影響批量操作性能的重要因素。假如Hibernate能直接執(zhí)行如下SQL語句:

  1. update CUSTOMERS set AGEAGE=AGE+1 where AGE>0;   

那么以上一條update語句就能更新CUSTOMERS表中的1萬條記錄。但是Hibernate并沒有直接提供執(zhí)行這種update語句的接口。應(yīng)用程序必須繞過Hibernate API,直接通過JDBC API來執(zhí)行該SQL語句:

  1. tx = session.beginTransaction();    
  2. Connection con=session.connection();    
  3. PreparedStatement stmt=con.prepareStatement("update CUSTOMERS set AGEAGE=AGE+1 "    
  4. +"where AGE>0 ");    
  5. stmt.executeUpdate();    
  6. tx.commit();  

以上程序演示了繞過Hibernate API,直接通過JDBC API訪問數(shù)據(jù)庫的過程。應(yīng)用程序通過Session的connection()方法獲得該Session使用的數(shù)據(jù)庫連接,然后通過它創(chuàng)建PreparedStatement對象并執(zhí)行SQL語句。值得注意的是,應(yīng)用程序仍然通過Hibernate的Transaction接口來聲明事務(wù)邊界。
如果底層數(shù)據(jù)庫(如Oracle)支持存儲過程,也可以通過存儲過程來執(zhí)行Hibernate批量更新。存儲過程直接在數(shù)據(jù)庫中運行,速度更加快。在Oracle數(shù)據(jù)庫中可以定義一個名為batchUpdateCustomer()的存儲過程,代碼如下:

  1. create or replace procedure batchUpdateCustomer(p_age in number) as    
  2. begin    
  3. update CUSTOMERS set AGEAGE=AGE+1 where AGE>p_age;    
  4. end;  

以上存儲過程有一個參數(shù)p_age,代表客戶的年齡,應(yīng)用程序可按照以下方式調(diào)用存儲過程:

  1. tx = session.beginTransaction();    
  2. Connection con=session.connection();    
  3. String procedure = "{call batchUpdateCustomer(?) }";    
  4. CallableStatement cstmt = con.prepareCall(procedure);    
  5. cstmt.setInt(1,0); //把年齡參數(shù)設(shè)為0    
  6. cstmt.executeUpdate();    
  7. tx.commit();  

從上面程序看出,應(yīng)用程序也必須繞過Hibernate API,直接通過JDBC API來調(diào)用存儲過程。
Session的各種重載形式的update()方法都一次只能更新一個對象,而delete()方法的有些重載形式允許以HQL語句作為參數(shù),例如:

  1. session.delete("from Customer c where c.age>0");  

如果CUSTOMERS表中有1萬條年齡大于零的記錄,那么以上代碼能刪除一萬條記錄。但是Session的delete()方法并沒有執(zhí)行以下delete語句

  1. delete from CUSTOMERS where AGE>0;  

Session的delete()方法先通過以下select語句把1萬個Customer對象加載到內(nèi)存中:

  1. select * from CUSTOMERS where AGE>0;  

接下來執(zhí)行一萬條delete語句,逐個刪除Customer對象:

  1. delete from CUSTOMERS where ID=i;    
  2. delete from CUSTOMERS where ID=j;    
  3. delete from CUSTOMERS where ID=k;  

由此可見,直接通過Hibernate API進行Hibernate批量更新和Hibernate批量刪除都不值得推薦。而直接通過JDBC API執(zhí)行相關(guān)的SQL語句或調(diào)用相關(guān)的存儲過程,是Hibernate批量更新和Hibernate批量刪除的***方式,這兩種方式都有以下優(yōu)點:

(1) 無需把數(shù)據(jù)庫中的大批量數(shù)據(jù)先加載到內(nèi)存中,然后逐個更新或修改它們,因此不會消耗大量內(nèi)存。
(2) 能在一條SQL語句中更新或刪除大批量的數(shù)據(jù)。

【編輯推薦】

  1. Hinerbate單端關(guān)聯(lián)代理頗析
  2. 簡述Hibernate中加載并存儲對象
  3. 深入了解Hibernate自動狀態(tài)檢測
  4. 教你如何在Hibernate中實例化集合和代理
  5. Hibernate之Session刷出(flush)
責(zé)任編輯:仲衡 來源: spaces
相關(guān)推薦

2010-02-23 09:33:39

Hibernate批量Hibernate批量

2009-09-24 09:45:23

Hibernate批量

2009-06-03 10:02:53

Hibernate批量刪除

2009-06-12 14:44:30

Hibernate A批量更新

2009-09-24 09:25:10

Hibernate批量

2010-09-03 11:47:38

SQL刪除

2009-09-27 14:33:01

Hibernate批量

2009-09-25 11:14:16

Hibernate批量

2010-09-01 16:26:11

SQL刪除批量

2010-11-22 15:34:17

MySQL多表更新

2011-04-29 09:15:10

Ubuntu 11.0

2010-02-04 14:06:01

Linux rpm包

2024-01-08 09:10:35

PostgreSQL數(shù)據(jù)庫管理系統(tǒng)

2009-09-28 10:05:27

Hibernate基礎(chǔ)

2011-05-26 15:53:59

數(shù)據(jù)庫更新維護

2023-07-26 07:18:54

死鎖線程池

2023-09-26 00:00:00

UpsertsClickHous更新

2010-09-02 10:53:21

SQL刪除

2019-12-20 14:56:50

批量刪除數(shù)據(jù)數(shù)據(jù)刪除

2010-09-01 15:43:57

點贊
收藏

51CTO技術(shù)棧公眾號