Posts Tagged ‘CRUD’
DBUnit and Hibernate: Beware of Collisions!
Here is an interesting case I have encountered this morning.
Context
I must test a Hibernate implementation of a DAO, using DBUnit and its XML dataset files as a minimal database. A CRUD unit test fails, I get this trace:
WARN util.JDBCExceptionReporter – SQL Error: 0, SQLState: null ERROR util.JDBCExceptionReporter – failed batch ERROR def.AbstractFlushingEventListener – Could not synchronize database state with session org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update |
Explanation
In my dataset, I had set some primary keys at 0, 1, …, 10. Owing to my Hibernate mapping, the primary keys are generated thanks to a sequence.
On another hand, with the CRUD test, the primary keys 0, 1, …, 10 are also assigned to newly created objects.
Here is the issue: there are collisions between the XML dataset file objects and the objects created with Hibernate, because obviously two objects never share the same primary key!