我想在同一个会话中执行几个 SELECT 和 DELETE 但 DELETE 返回应该删除的行没有被删除,为什么?谢谢
跟踪: - 历史清除:10 个要通过查询删除的条目 - 自 2013 年 10 月 14 日 13:07:28.0 以来的历史清除:0 个条目被查询删除
源代码:
@Override
public void removeAllHistoryOlderThan(Date since) {
try
{
int deletedEntities = 0;
int nbRetries = 0;
Session session = getHibernateTemplate().getSessionFactory().openSession();
Query query = session.createQuery("SELECT h FROM History h WHERE h.operationDate < :since ORDER BY h.operationDate ASC").setDate("since", since).setMaxResults(10);
List list = query.list();
while ( (list.size() != 0)
&& (nbRetries < 20))
{
logger.info("History purge : " + list.size() + " entries to remove by query");
Transaction tx = session.beginTransaction();
Date maxSince = ((History)list.get(list.size()-1)).getOperationDate();
int tmpDeletedEntities = session.createQuery("DELETE History h WHERE h.operationDate < :since")
.setDate("since", maxSince)
.executeUpdate();
tx.commit();
logger.info("History purge since " + maxSince + " : " + tmpDeletedEntities + " entries removed by query");
deletedEntities += tmpDeletedEntities;
nbRetries++;
list = query.list();
}
session.close();
logger.info("History purge since " + since + " : Total = " + deletedEntities + " entries removed");
}
catch (HibernateException e)
{
logger.error(e.getMessage());
}
}