在Hibernate應用中實現大量操作

來源:互聯網
上載者:User

在Hibernate應用中實現大量操作
最近的一個項目中,作一個查詢的時候需求批次更新一下資料,然後在查詢。
在Hiberante應用中,這個更新操作

一、session.update(object)
一個方法是,根據條件載入出一個list,如果合格有上萬或更多,會載入這麼多個對象到sessin緩衝中
然後遍曆,對每個對象挨個更新。當事務提交是會清理緩衝,同時執行上萬個update語句
代碼 tx = session.beginTransaction();    Iterator objects =session.find("from Project where p.id>0").iterator();    while(objects .hasNext()){    Object object=(Object)objects .next();    object.setXXX();    }     tx.commit();    session.close();  
以上批次更新方式有兩個缺點:
(1) 佔用大量記憶體,必須把所有合格對象先載入到記憶體,然後一一更新它們。
(2) 執行的update語句的數目太多,每個update語句只能更新一個Customer對象,頻繁的訪問資料庫,會降低應用的效能。
為了迅速釋放對象佔用的記憶體,可以在更新每個對象後,就調用Session的evict()方法立即釋放它的記憶體:
代碼 tx = session.beginTransaction();    Iterator objects=session.find(“hql").iterator();    while(objects.hasNext()){    Object  object =(Object) objects.next();    object.setXXX();    session.flush();    session.evict(customer);    }     tx.commit();    session.close();  
flush()方法使Hibernate立刻根據這個object對象的狀態變化同步更新資料庫,從而立即執行相關的update語句;evict()方法用於把這個object對象從緩衝中清除出去,從而及時釋放它佔用的記憶體。
可以稍微提高一些效能,但還是要產生上萬個update語句,這個影響大量操作的重要因素。

 

二、直接執行sql
如果hibernate能執行執行一個update語句,那麼合格對象將一次性更新好。
但是Hibernate並沒有直接提供執行這種update語句的介面。應用程式必須繞過Hibernate API,直接通過JDBC API來執行該SQL語句:
代碼 tx = session.beginTransaction();    Connection con=session.connection();    PreparedStatement stmt=con.prepareStatement(sql");    stmt.executeUpdate();    tx.commit();   
以上程式示範了繞過Hibernate API,直接通過JDBC API訪問資料庫的過程。應用程式通過Session的connection()方法獲得該Session使用的資料庫連接,然後通過它建立PreparedStatement對象並執行SQL語句。值得注意的是,應用程式仍然通過Hibernate的Transaction介面來聲明事務邊界。

 

三、使用預存程序
如果底層資料庫(如Oracle)支援預存程序,也可以通過預存程序來執行批次更新。預存程序直接在資料庫中運行,速度更加快。
代碼 create or replace procedure updateProject is    begin      update project p set p.total_intend_gather =       (select sum(ig.gather_sum) from intend_gather ig where ig.project_number=p.project_number);            update project p set p.total_actual_gather =       (select sum(ag.gahter_sum) from actual_gather ag where ag.project_number=p.project_number);            update project p set p.total_invoice=    (select sum(invoice.invoice_sum) from invoice invoice     where invoice.intend_id in     (select ig.intend_id  from intend_gather ig where ig.project_number=p.project_number));         end updateProject;   

 

調用代碼 代碼 Session session = this.getSession();    Transaction tx =null;    try {        tx = session.beginTransaction();        Connection con = session.connection();        String procedure = "{call updateproject() }";        CallableStatement cstmt = con.prepareCall(procedure);        cstmt.executeUpdate();        tx.commit();       } catch (Exception e) {       tx.rollback();    }   

 

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

四、delete操作
Session的各種重載形式的update()方法都一次只能更新一個對象,而delete()方法的有些重載形式允許以HQL語句作為參數,例如:
代碼 session.delete(from Project where p.id>0);  
能夠實現大量刪除,但是令人失望的是Session的delete()方法並沒有執行以下delete語句
代碼 delete from PROJECT where ID>0;  
而是
Session的delete()方法先通過以下select語句把所有符合的對象載入到記憶體中:
代碼 select * from Project where ID>0;  
接下來執行N多個delete語句,逐個刪除project對象:
代碼 delete from PROJECT where ID=i;    delete from PROJECT where ID=j;    delete from PROJECT where ID=k;  

 

所以,直接通過Hibernate API進行批次更新和大量刪除都不爽。而直接通過JDBC API執行相關的SQL語句或調用相關的預存程序,是批次更新和大量刪除的最佳方式,這兩種方式都有以下優點:
(1) 無需把資料庫中的大批量資料先載入到記憶體中,然後逐個更新或修改它們,因此不會消耗大量記憶體。
(2) 能在一條SQL語句中更新或刪除大批量的資料。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.