Hibernate怎样实现批量修改和删除

hibernate的批量删除对于提高hibernate性能来说确实很重要,所以我总结整理了一下,大体的实现代码是:
批量修改:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String hqlUpdate = “update Customer set name = :newName where name=ldName”;
int updatedEntities = s.createQuery( hqlUpdate )
.setString( “newName”, newName )
.setString( “oldName”, oldName )
.executeUpdate();
tx.commit();
session.close();
批量删除:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String hqlDelete = “delete Customer where name =ldName”;
int deletedEntities = s.createQuery( hqlDelete )
.setString( “oldName”, oldName )
.executeUpdate();
tx.commit();
session.close();
也可以使用JDBC API来操作:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Connection con=session.connection();
PreparedStatement stmt=con.prepareStatement(“update Customer set name = :newName where name=ldName”);
stmt.executeUpdate();
tx.commit();
session.close();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Connection con=session.connection();
PreparedStatement stmt=con.prepareStatement(“delete Customer where name =ldName”);
stmt.executeUpdate();
tx.commit();
session.close(); 本文链接地址: Hibernate怎样实现批量修改和删除