JPA DAO integration test not throwing exception when duplicate object saved?
        Posted  
        
            by HDave
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by HDave
        
        
        
        Published on 2010-06-14T16:10:07Z
        Indexed on 
            2010/06/14
            21:22 UTC
        
        
        Read the original article
        Hit count: 313
        
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
© Stack Overflow or respective owner