当前位置: 移动技术网 > IT编程>开发语言>Java > jsp Hibernate批量更新和批量删除处理代码

jsp Hibernate批量更新和批量删除处理代码

2017年12月12日  | 移动技术网IT编程  | 我要评论
以下程序直接通过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) asbeginupdate 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); //把年龄参数设为0cstmt.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语句中更新或删除大批量的数据。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网