Hibernate batch update: need to know the failed statement
I have a code looking like this:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
for ( Customer customer: customers ) {
i++;
session.update(customer);
if ( i % 200 == 0 ) { //200, same as the JDBC batch size
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}
} catch (Exc e) {
//TODO want to know customer id here!
}
tx.commit();
session.close();
Say, at some point session.flush() raises an DataException, because one of
the fields did not map into the database column size, one of those batch
of 200 customers. Nothing wrong with it, data can be corrupted, it's ok.
BUT, I really need to know the customer id which failed. Database returns
meaningless error message, not stating what was the params of the
statement, etc. Catched exception also does not contain which customer did
fail, only the sql statement text, looking like 'update Customer set
name=?'
Can I somehow determine it using the hibernate session? Does it store
somewhere the information about last entity it tried to save down?
No comments:
Post a Comment