How to properly test for constraint violation in hibernate?
Posted
by Cesar
on Stack Overflow
See other posts from Stack Overflow
or by Cesar
Published on 2010-04-20T23:14:38Z
Indexed on
2010/04/20
23:33 UTC
Read the original article
Hit count: 1007
I'm trying to test Hibernate mappings, specifically a unique constraint. My POJO is mapped as follows:
<property name="name" type="string" unique="true" not-null="true" />
What I want to do is to test that I can't persist two entities with the same name:
@Test(expected=ConstraintViolationException.class)
public void testPersistTwoExpertiseAreasWithTheSameNameIsNotAllowed(){
ExpertiseArea ea = new ExpertiseArea("Design");
ExpertiseArea otherEA = new ExpertiseArea("Design");
ead.setSession(getSessionFactory().getCurrentSession());
ead.getSession().beginTransaction();
ead.makePersistent(ea);
ead.makePersistent(otherEA);
ead.getSession().getTransaction().commit();
}
On commiting the current transaction, I can see in the logs that a ConstraintViolationException
is thrown:
16:08:47,571 DEBUG SQL:111 - insert into ExpertiseArea (VERSION, name, id) values (?, ?, ?)
Hibernate: insert into ExpertiseArea (VERSION, name, id) values (?, ?, ?)
16:08:47,571 DEBUG SQL:111 - insert into ExpertiseArea (VERSION, name, id) values (?, ?, ?)
Hibernate: insert into ExpertiseArea (VERSION, name, id) values (?, ?, ?)
16:08:47,572 WARN JDBCExceptionReporter:100 - SQL Error: -104, SQLState: 23505
16:08:47,572 ERROR JDBCExceptionReporter:101 - integrity constraint violation: unique constraint or index violation; SYS_CT_10036 table: EXPERTISEAREA
16:08:47,573 ERROR AbstractFlushingEventListener:324 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
So I would expect the test to pass, since the expected ConstraintViolationException is thrown. However, the test never completes (neither pass nor fails) and I have to manually kill the test runner.
What's the correct way to test this?
© Stack Overflow or respective owner