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 康文 阅读(456) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航:
 
<2006年9月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜