hibernate 2.1没有对批量更新和批量删除提供很好的支持,而 hibernate 3.0 提供了支持,能够直接执行批量更新或批量删除语句,无需把被更新或删除的对象先加载到内存中。
update
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String hqlUpdate = "update Customer set name = :newName where name=:oldName";
int updatedEntities = session.createQuery( hqlUpdate )
.setString("newName",newName)
.setString("oldName",oldName)
.executeUpdate();
tx.commit();
session.close();
delete
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String hqlDelete = "delete Customer where name = :oldName";
int deletedEntities = session.createQuery( hqlDelete )
.setString("oldName",oldName)
.executeUpdate();
tx.commit();
session.close();