Hibernate批量更新和批量删除实战

开发 后端
Hibernate批量更新是指在一个事务中更新大批量数据,Hibernate批量删除是指在一个事务中删除大批量数据。以下程序直接通过Hibernate API批量更新CUSTOMERS表中年龄大于零的所有记录的AGE字段:

本文向大家介绍Hibernate批量更新和Hibernate批量删除,可能好多人还不了解Hibernate批量更新和Hibernate批量删除,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。

Hibernate批量更新是指在一个事务中更新大批量数据,Hibernate批量删除是指在一个事务中删除大批量数据。以下程序直接通过Hibernate API批量更新CUSTOMERS表中年龄大于零的所有记录的AGE字段:

  1. tx = session.beginTransaction();    
  2. ??Iterator customers=session.find("from Customer c where c.age>0").iterator();    
  3. ??while(customers.hasNext()){    
  4. ??Customer customer=(Customer)customers.next();    
  5. ??customer.setAge(customer.getAge()+1);    
  6. ??}     
  7. ??tx.commit();    
  8. ??session.close();  

如果CUSTOMERS表中有1万条年龄大于零的记录,那么Session的find()方法会一下子加载1万个Customer对象到内存。当执行tx.commit()方法时,会清理缓存,Hibernate执行1万条更新CUSTOMERS表的update语句:

  1. update CUSTOMERS set AGE=? …. where ID=i;    
  2. ??update CUSTOMERS set AGE=? …. where ID=j;    
  3. ??update CUSTOMERS set AGE=? …. where ID=k;  

以上Hibernate批量更新方式有两个缺点:

(1) 占用大量内存,必须把1万个Customer对象先加载到内存,然后一一更新它们。
(2) 执行的update语句的数目太多,每个update语句只能更新一个Customer对象,必须通过1万条update语句才能更新一万个Customer对象,频繁的访问数据库,会大大降低应用的性能。

为了迅速释放1万个Customer对象占用的内存,可以在更新每个Customer对象后,就调用Session的evict()方法立即释放它的内存:

  1. tx = session.beginTransaction();    
  2. Iterator customers=session.find("from Customer c where c.age>0").iterator();    
  3. while(customers.hasNext()){    
  4. Customer customer=(Customer)customers.next();    
  5. customer.setAge(customer.getAge()+1);    
  6. session.flush();    
  7. session.evict(customer);    
  8. }     
  9. tx.commit();    
  10. session.close();  

在以上程序中,修改了一个Customer对象的age属性后,就立即调用Session的flush()方法和evict()方法,flush()方法使Hibernate立刻根据这个Customer对象的状态变化同步更新数据库,从而立即执行相关的update语句;evict()方法用于把这个Customer对象从缓存中清除出去,从而及时释放它占用的内存。

但evict()方法只能稍微提高批量操作的性能,因为不管有没有使用evict()方法,Hibernate都必须执行1万条update语句,才能更新1万个Customer对象,这是影响批量操作性能的重要因素。假如Hibernate能直接执行如下SQL语句:

  1. update CUSTOMERS set AGEAGE=AGE+1 where AGE>0;   

那么以上一条update语句就能更新CUSTOMERS表中的1万条记录。但是Hibernate并没有直接提供执行这种update语句的接口。应用程序必须绕过Hibernate API,直接通过JDBC API来执行该SQL语句:

  1. tx = session.beginTransaction();    
  2. Connection con=session.connection();    
  3. PreparedStatement stmt=con.prepareStatement("update CUSTOMERS set AGEAGE=AGE+1 "    
  4. +"where AGE>0 ");    
  5. stmt.executeUpdate();    
  6. tx.commit();  

以上程序演示了绕过Hibernate API,直接通过JDBC API访问数据库的过程。应用程序通过Session的connection()方法获得该Session使用的数据库连接,然后通过它创建PreparedStatement对象并执行SQL语句。值得注意的是,应用程序仍然通过Hibernate的Transaction接口来声明事务边界。
如果底层数据库(如Oracle)支持存储过程,也可以通过存储过程来执行Hibernate批量更新。存储过程直接在数据库中运行,速度更加快。在Oracle数据库中可以定义一个名为batchUpdateCustomer()的存储过程,代码如下:

  1. create or replace procedure batchUpdateCustomer(p_age in number) as    
  2. begin    
  3. update CUSTOMERS set AGEAGE=AGE+1 where AGE>p_age;    
  4. end;  

以上存储过程有一个参数p_age,代表客户的年龄,应用程序可按照以下方式调用存储过程:

  1. tx = session.beginTransaction();    
  2. Connection con=session.connection();    
  3. String procedure = "{call batchUpdateCustomer(?) }";    
  4. CallableStatement cstmt = con.prepareCall(procedure);    
  5. cstmt.setInt(1,0); //把年龄参数设为0    
  6. cstmt.executeUpdate();    
  7. tx.commit();  

从上面程序看出,应用程序也必须绕过Hibernate API,直接通过JDBC API来调用存储过程。
Session的各种重载形式的update()方法都一次只能更新一个对象,而delete()方法的有些重载形式允许以HQL语句作为参数,例如:

  1. session.delete("from Customer c where c.age>0");  

如果CUSTOMERS表中有1万条年龄大于零的记录,那么以上代码能删除一万条记录。但是Session的delete()方法并没有执行以下delete语句

  1. delete from CUSTOMERS where AGE>0;  

Session的delete()方法先通过以下select语句把1万个Customer对象加载到内存中:

  1. select * from CUSTOMERS where AGE>0;  

接下来执行一万条delete语句,逐个删除Customer对象:

  1. delete from CUSTOMERS where ID=i;    
  2. delete from CUSTOMERS where ID=j;    
  3. delete from CUSTOMERS where ID=k;  

由此可见,直接通过Hibernate API进行Hibernate批量更新和Hibernate批量删除都不值得推荐。而直接通过JDBC API执行相关的SQL语句或调用相关的存储过程,是Hibernate批量更新和Hibernate批量删除的***方式,这两种方式都有以下优点:

(1) 无需把数据库中的大批量数据先加载到内存中,然后逐个更新或修改它们,因此不会消耗大量内存。
(2) 能在一条SQL语句中更新或删除大批量的数据。

【编辑推荐】

  1. Hinerbate单端关联代理颇析
  2. 简述Hibernate中加载并存储对象
  3. 深入了解Hibernate自动状态检测
  4. 教你如何在Hibernate中实例化集合和代理
  5. Hibernate之Session刷出(flush)
责任编辑:仲衡 来源: spaces
相关推荐

2010-02-23 09:33:39

Hibernate批量Hibernate批量

2009-09-24 09:45:23

Hibernate批量

2009-06-03 10:02:53

Hibernate批量删除

2009-06-12 14:44:30

Hibernate A批量更新

2009-09-24 09:25:10

Hibernate批量

2010-09-03 11:47:38

SQL删除

2009-09-25 11:14:16

Hibernate批量

2009-09-27 14:33:01

Hibernate批量

2010-09-01 16:26:11

SQL删除批量

2010-11-22 15:34:17

MySQL多表更新

2011-04-29 09:15:10

Ubuntu 11.0

2010-02-04 14:06:01

Linux rpm包

2024-01-08 09:10:35

PostgreSQL数据库管理系统

2023-09-26 00:00:00

UpsertsClickHous更新

2011-05-26 15:53:59

数据库更新维护

2023-07-26 07:18:54

死锁线程池

2009-09-28 10:05:27

Hibernate基础

2019-12-20 14:56:50

批量删除数据数据删除

2010-09-02 10:53:21

SQL删除

2011-07-11 13:22:28

存储过程
点赞
收藏

51CTO技术栈公众号