hibernate 一級緩存

          1 hibernate 一級緩存
          Session?
          ? evict(Object o) 從緩存中清除指定的持久化對象
          ? clear()???????? 清除緩存中所有對象
          2 批量更新于批量刪除
          ? 1) 批量更新
          ?? Iterator customers=session.find("from Customer c where c.age>0");
          ?? while(customers.hasNext()){
          ???? Customer customer=(Customer)customers.next();
          ???? customer.setAge(customer.getAge()+1);
          ?? }
          ?? tx.commit();
          ?? session.close();

          ? 缺點:內存中加載了大量數據
          ??????? 執行了多次update 語句
          ?
          ?? 改進
          ?? Iterator customers=session.find("from Customer c where c.age>0");
          ?? while(customers.hasNext()){
          ???? Customer customer=(Customer)customers.next();
          ???? customer.setAge(customer.getAge()+1);
          ???? session.flush();
          ???? session.evict(customer);
          ?? }
          ?? tx.commit();
          ?? session.close();
          ?? 遺留問題
          ?? 執行了多次update 語句
          ??
          ?? 采用jdbc api 進行調用
          ?? Connection con=session.connection();
          ?? PrepareStatement stmt=con.prepareStatement("update customers set age=age+1 where age>0");
          ?? stmt.executeUpdate();
          ?? tx.commit();
          ?? 另外,也可以調用底層的存儲過程進行批量更新
          ?? create or replace procedure batchUpdateCustomer(p_age,in number) as
          ?? begin
          ????? update customer set age=age+1 where age>p_age;
          ?? end;
          ??
          ?? tx=session.beginTransaction();
          ?? Connection con=session.connection();
          ?? CallableStatement cstmt=con.prepareCall(batchUpdateCustomer);
          ?? cstmt.setInt(1,0);
          ?? cstmt.eqecuteUpdate();
          ?? tx.commit();
          ?? 2) 批量數據的刪除
          ??? session.delete("from? Customer c where c.age>0");
          ??? 實際調用的過程
          ??? session * from Customer where age>0;
          ??? 在把所有數據加載到內存之后執行多條delete 語句
          ??? delete from customer where id=i;
          ???? .......................
          ?? 改進辦法采用jdbc api 進行批量數據的刪除
          ?????
          ?? tx=session.beginTransaction();
          ?? Connection con=session.connection();
          ?? con.execute("delete from customers where age>0");
          ?? tx.commit();

          posted on 2006-09-14 09:22 康文 閱讀(466) 評論(0)  編輯  收藏 所屬分類: java

          <2006年9月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 吴桥县| 固原市| 沛县| 策勒县| 琼海市| 蓬安县| 耒阳市| 峨眉山市| 轮台县| 旺苍县| 安丘市| 大连市| 喀喇| 来凤县| 明水县| 米脂县| 夏邑县| 博爱县| 克什克腾旗| 庆安县| 富民县| 阿瓦提县| 淮阳县| 加查县| 米泉市| 安图县| 嘉善县| 安乡县| 中方县| 六枝特区| 德令哈市| 河间市| 新宾| 舟曲县| 怀柔区| 江口县| 安庆市| 汾阳市| 长寿区| 济南市| 阿克苏市|