hibernate 批量删除 更新小节

来源:互联网 发布:算法第四版官网 编辑:程序博客网 时间:2024/05/02 01:18
一。更新 可以先从数据库把数据取出 通过iterator 指针一条一条取出,然后单个更新。更新时将每个动作都flush 就不会占有太大内存。如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);}evit方法将对象从缓存中清除。二。删除也可以通过这种方式hibernate的api在删除时先查询出所有的内容在删除,占有大量的内存。三。通过jdbc更新 删除。例子 用orcle的存储过程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();

原创粉丝点击