我有一個對象列表,對於我正在執行的每個對象Session.update(),何時應該調用Session.flush()?列表完成迭代後或每次更新後?該代碼段爲:在Hibernate會話批量更新中調用Session.flush
public void updateUserAssignmentInfo(final long itemId) {
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
session.flush();
session.setCacheMode(CacheMode.IGNORE);
List<UserAssignmentInfo> userAssignmentInfos = session.createQuery("from UserAssignmentInfo as userAssignmentInfo where userAssignmentInfo.itemId = " + itemId).list();
if (userAssignmentInfos.size() == 0) {
return null;
}
for (UserAssignmentInfo userAssignmentInfo : userAssignmentInfos) {
userAssignmentInfo.setIsCurrentlyAssigned(false);
session.update(userAssignmentInfo);
// should I call flush here?
}
session.flush(); // or here?
return null;
}
});
}
謝謝。您提供的鏈接非常有用。 – 2011-04-08 13:39:05