字体大小: 正文
Hibernate笔记=>批量更新和删除问题(2006-08-16 10:56:53)

在一个事务中更新大批量数据或删除大批量数据时,Hibernate是存在性能缺陷的。
以更新为例,删除也是同理如:
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();
如果有1万条符合条件的记录,那么session.find()会加载1万个Customer对象到内存中,当执行tx.commit()时,Hibernate会执行1万次更新CUSTOMERS表的update语句:
update CUSTOMRES set AGE=? ... where ID=1;
update CUSTOMRES set AGE=? ... where ID=2;
......
update CUSTOMRES set AGE=? ... where ID=10000;
可见既浪费了内存,又浪费了效率。实际上不需要把1万个对象加载到内存,因为它们将不被使用,同时置需要一个SQL语句即可
update CUSTOMERS set AGE=? ... where AGE>0;
但是很可惜的是Hibernate没有直接提供这样的update接口,所以应用程序必须绕过HiberateAPI,直接通过JDBC API来执行SQL语句:
tx=session.beginTransaction();
Connection con = session.connection();
PreparedStatement stmt = con.prepareStatement(
      "update CUSTOMERS set AGE=AGE+1 where AGE>0");
stmt.excuteUpdate();
tx.commit();
这样就绕过了HibernateAPI,值得注意的是,应用程序仍然通过Hiberate的Transaction接口来生命事务边界。
 
如果是存储过程,那么执行效率更高一些
tx=session.beginTransation();
Connection con = session.connection();
String procedure = "{call batchUpdateCustomer(?)}";
CallableStatement cstmt= conn.prepareCall(procedure);
cstmt.setInt(1,0);//设置第一个参数为0
cstmt.excuteUpdate();
tx.commit();
以上假定已经在DB中定义了一个存储过程
procedure batchUpdateCustomer(p_age in number)
 




其他相关文章

Hibernate组成关系的映射
Hibernate批量更新和删除问题
Hibernate缓存技术
Hibernate Java/.net对象的状态
Hibernate Session缓存
Hibernate 检索策略
Hibernate 检索相关细节问题
Hibernate 继承关系的映射
Hibernate 一对多关系的映射
Hibernate 对象与关系的映射基础
Hibernate跑起来
Hibernate的核心接口

 

加载中,请稍候...
  • 评论加载中,请稍候...

验证码:请点击后输入验证码  收听验证码

发评论

以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

相关博文
读取中...
推荐博文
读取中...