wml

          Z:在Hibernate應用中如何處理批量更新和批量刪除

          在Hibernate應用中如何處理批量更新和批量刪除?

          選自<<精通Hibernate:Java對象持久化技術詳解>> 作者:孫衛琴 來源:www.javathinker.org
          如果轉載,請標明出處,謝謝

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

          tx = session.beginTransaction();
          Iterator customers=session.find("from Customer c where c.age>0").iterator();
          while(customers.hasNext()){
          Customer customer=(Customer)customers.next();
          customer.setAge(customer.getAge()+1);
          }

          tx.commit();
          session.close();

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

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

          以上批量更新方式有兩個缺點:
          (1) 占用大量內存,必須把1萬個Customer對象先加載到內存,然后一一更新它們。
          (2) 執行的update語句的數目太多,每個update語句只能更新一個Customer對象,必須通過1萬條update語句才能更新一萬個Customer對象,頻繁的訪問數據庫,會大大降低應用的性能。

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

          tx = session.beginTransaction();
          Iterator customers=session.find("from Customer c where c.age>0").iterator();
          while(customers.hasNext()){
          Customer customer=(Customer)customers.next();
          customer.setAge(customer.getAge()+1);
          session.flush();
          session.evict(customer);
          }

          tx.commit();
          session.close();

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

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

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

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

          tx = session.beginTransaction();

          Connection con=session.connection();
          PreparedStatement stmt=con.prepareStatement("update CUSTOMERS set AGE=AGE+1 "
          +"where AGE>0 ");
          stmt.executeUpdate();

          tx.commit();

          以上程序演示了繞過Hibernate API,直接通過JDBC API訪問數據庫的過程。應用程序通過Session的connection()方法獲得該Session使用的數據庫連接,然后通過它創建PreparedStatement對象并執行SQL語句。值得注意的是,應用程序仍然通過Hibernate的Transaction接口來聲明事務邊界。

          如果底層數據庫(如Oracle)支持存儲過程,也可以通過存儲過程來執行批量更新。存儲過程直接在數據庫中運行,速度更加快。在Oracle數據庫中可以定義一個名為batchUpdateCustomer()的存儲過程,代碼如下:

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

          以上存儲過程有一個參數p_age,代表客戶的年齡,應用程序可按照以下方式調用存儲過程:

          tx = session.beginTransaction();
          Connection con=session.connection();

          String procedure = "{call batchUpdateCustomer(?) }";
          CallableStatement cstmt = con.prepareCall(procedure);
          cstmt.setInt(1,0); //把年齡參數設為0
          cstmt.executeUpdate();
          tx.commit();

          從上面程序看出,應用程序也必須繞過Hibernate API,直接通過JDBC API來調用存儲過程。

          Session的各種重載形式的update()方法都一次只能更新一個對象,而delete()方法的有些重載形式允許以HQL語句作為參數,例如:

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

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

          delete from CUSTOMERS where AGE>0;

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

          select * from CUSTOMERS where AGE>0;

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

          delete from CUSTOMERS where ID=i;
          delete from CUSTOMERS where ID=j;
          ……
          delete from CUSTOMERS where ID=k;

          由此可見,直接通過Hibernate API進行批量更新和批量刪除都不值得推薦。而直接通過JDBC API執行相關的SQL語句或調用相關的存儲過程,是批量更新和批量刪除的最佳方式,這兩種方式都有以下優點:

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

          Hibernate3.0對批量更新和批量刪除提供了支持,能夠直接執行批量更新或批量刪除語句,無需把被更新或刪除的對象先加載到內存中。以下是通過Hibernate3.0執行批量更新的程序代碼:

          代碼
          1. Session session = sessionFactory.openSession();    
          2. Transaction tx = session.beginTransaction();    
          3. String hqlUpdate =  "update Customer set name = :newName where name =<img src=" /images/forum/smiles/icon_surprised.gif "/>ldName" ;    
          4. int  updatedEntities = s.createQuery( hqlUpdate )    
          5. .setString(  "newName" , newName )    
          6. .setString(  "oldName" , oldName )    
          7. .executeUpdate();    
          8. tx.commit();    
          9. session.close();   

          以下是通過Hibernate3.0執行批量刪除的程序代碼:

          代碼
          1. Session session = sessionFactory.openSession();    
          2. Transaction tx = session.beginTransaction();    
          3. String hqlDelete =  "delete Customer where name =<img src=" /images/forum/smiles/icon_surprised.gif "/>ldName" ;    
          4. int  deletedEntities = s.createQuery( hqlDelete )    
          5. .setString(  "oldName" , oldName )    
          6. .executeUpdate();    
          7. tx.commit();    
          8. session.close();   

          posted on 2006-11-28 16:04 wml 閱讀(397) 評論(0)  編輯  收藏 所屬分類: Hibernate

          主站蜘蛛池模板: 仙居县| 剑川县| 墨竹工卡县| 乡城县| 舟曲县| 电白县| 镇赉县| 大理市| 海门市| 淮滨县| 皮山县| 宜春市| 宝兴县| 江油市| 临夏县| 石嘴山市| 托里县| 裕民县| 玉龙| 东乡| 榆林市| 桂平市| 南昌市| 肃南| 达州市| 南宁市| 宁津县| 长沙市| 乌兰察布市| 昆山市| 临洮县| 东平县| 阿拉尔市| 进贤县| 秦安县| 时尚| 响水县| 项城市| 江门市| 临夏县| 满城县|