I want to be able to select some rows from a table, update a value in a column and persist this update back to the db for each row.
Code:
ScrollableResults cashflows = query.setParameter("farDate", farDate, Hibernate.DATE).
setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
int count=0;
ArrayList<PendingCashFlow> results = new ArrayList<PendingCashFlow>();
while(cashflows.next()) {
PendingCashFlow pcf = (PendingCashFlow) cashflows.get(0);
pcf.setProcessingState(ProcessingState.PROCESSING);
session.update(pcf);
results.add(pcf);
if ( ++count % 50 == 0 ) {
session.flush();
}
}
session.flush();
cashflows.close();
In the code above, setting the ProcessingState means that other processes won't select the row and attempt to process it. Further processing will then take place, and at a later point I want to delete these rows from the table.
I call getHibernateTemplate().deleteAll() passing in the list of PendingCashFlows obtained from the code fragment above. When this line of code is executed, the method doesn't return, and no rows are deleted or exception thrown. If I return the PendingCashFlows from a regular findByNamedQuery call, and don't update anything, then the call to deleteAll succeeds.
The only reason I can think of for this happening is that the PendingCashFlows are not bound to the session by the batch update, but I haven't called session.clear() to clear the persistence context, so I'm baffled as to what the problem is. Can anyone shed light on this?