0

I would like in a same session execute several SELECT and DELETE but the DELETE returns that no row are deleted while it should be, why ? Thanks

Traces: - History purge : 10 entries to remove by query - History purge since 2013-10-14 13:07:28.0 : 0 entries removed by query

Source code:

@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());
    }
}
  • Is the maxSince date correct ("2013-10-14 13:07:28.0")? Are there any entities in db with operationDate < maxSince? If you're logging sql queries generated by hibernate (http://stackoverflow.com/questions/1710476/print-query-string-in-hibernate-with-parameter-values) you can try to execute them manually and see what's wrong with them. – Aneta Stępień Oct 22 '13 at 20:27
  • Thanks I have activate logging and my mystake was the use of "setDate" (which truncate maxSince to "2013-10-14 00:00:00.0") instead of "setTimestamp" – GloupGloup Oct 23 '13 at 09:53

1 Answers1

0

The function "setDate" truncate the parameter maxSince, I have changed my query using "setTimStamp"

int tmpDeletedEntities = session.createQuery("DELETE History h WHERE h.operationDate < :since")
.setTimestamp("since", maxSince)
.executeUpdate();