Testing with Hibernate

April 08, 2010

Some of the unit tests I wrote lately involved setting up a data setup in the DB and testing the code using and mutating it. To make it clear, the DB is an in memory DB so the setup is super fast without IO slowdowns. Its very easy to do the setup using hibernate but the problem comes when you set a large collection of objects and a “java.sql.BatchUpdateException: failed batch” is being thrown. The frustrating point there is that hibernate won’t let you know what exactly went wrong, even if you set your logger to “trace” level.

In order to solve it you can add the following to the system properties of that test:

-Dhibernate.statement_cache.size=0 
-Dhibernate.jdbc.batch_size=0 
-Dhibernate.jdbc.use_scrollable_resultset=false 
-Dhibernate.bytecode.use_reflection_optimizer=false 
-Dhibernate.show_sql=true

It will execute the statements one by one and will give more precise details of what went wrong.
Note: you do NOT want to use these properties in production or in the continuance integration (CI) environment.