herbernate 批量更新

Session session = null;
try {
   session = getSession();
   String hqlUpdate = "update com.xes.jzh.core.domain.ClassGroup set is_cancel_stick = :isCancelStick, update_time = :updateTime where group_id = :groupId";
   ClassGroup obj = null;
   for (int i = 0; i < list.size(); i++) {
      obj = list.get(i);
      obj.setIsCancelStick("1");
      session.createQuery(hqlUpdate)
            .setString( "isCancelStick", "1" )
            .setString( "updateTime", System.currentTimeMillis()+"" )
            .setString( "groupId", obj.getGroupId())
            .executeUpdate();
      if (i % 20 == 0) {
         session.flush();
         session.clear();
      }
   }
   session.flush();
   session.clear();
} catch (Exception e) {
   session.getTransaction().rollback();
   throw new RuntimeException(e);

} finally {
   closeSession(session);
}

猜你喜欢

转载自blog.csdn.net/mazhenxing0805/article/details/80082792