I am in the process of unit testing a DAO built with Spring/JPA and Hibernate as the provider.
Prior to running the test, DBUnit inserted a User record with username "poweruser" -- username is the primary key in the users table. Here is the integration test method:
@Test
@ExpectedException(EntityExistsException.class)
public void save_UserTestDataSaveUserWithPreExistingId_EntityExistsException() {
User newUser = new UserImpl("poweruser");
newUser.setEmail("
[email protected]");
newUser.setFirstName("New");
newUser.setLastName("User");
newUser.setPassword("secret");
dao.persist(newUser);
}
I have verified that the record is in the database at the start of this method. Not sure if this is relevant, but if I do a dao.flush() at the end of this method I get the following exception:
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException:
Could not execute JDBC batch update