HHH-13300 Test behavior when Query.executeUpdate() throws an exception

(cherry picked from commit 5a1efe883e)
This commit is contained in:
Gail Badner 2019-03-19 23:41:27 -07:00 committed by gbadner
parent a3433be822
commit a93a5183ba
2 changed files with 36 additions and 0 deletions

View File

@ -94,6 +94,13 @@ interface ExceptionExpectations {
assertThat( e.getCause(), instanceOf( PersistenceException.class ) ); assertThat( e.getCause(), instanceOf( PersistenceException.class ) );
assertThat( e.getCause().getCause(), instanceOf( TransactionException.class ) ); assertThat( e.getCause().getCause(), instanceOf( TransactionException.class ) );
} }
@Override
public void onExecuteUpdateWithConstraintViolation(RuntimeException e) {
assertThat( e, instanceOf( PersistenceException.class ) );
assertThat( e.getCause(), instanceOf( ConstraintViolationException.class ) );
assertThat( e.getCause().getCause(), instanceOf( SQLException.class ) );
}
}; };
} }
@ -160,6 +167,12 @@ interface ExceptionExpectations {
public void onTransactionExceptionOnCommit(RuntimeException e) { public void onTransactionExceptionOnCommit(RuntimeException e) {
assertThat( e, instanceOf( TransactionException.class ) ); assertThat( e, instanceOf( TransactionException.class ) );
} }
@Override
public void onExecuteUpdateWithConstraintViolation(RuntimeException e) {
assertThat( e, instanceOf( ConstraintViolationException.class ) );
assertThat( e.getCause(), instanceOf( SQLException.class ) );
}
}; };
} }
@ -233,6 +246,13 @@ interface ExceptionExpectations {
assertThat( e, instanceOf( PersistenceException.class ) ); assertThat( e, instanceOf( PersistenceException.class ) );
assertThat( e.getCause(), instanceOf( TransactionException.class ) ); assertThat( e.getCause(), instanceOf( TransactionException.class ) );
} }
@Override
public void onExecuteUpdateWithConstraintViolation(RuntimeException e) {
assertThat( e, instanceOf( PersistenceException.class ) );
assertThat( e.getCause(), instanceOf( ConstraintViolationException.class ) );
assertThat( e.getCause().getCause(), instanceOf( SQLException.class ) );
}
}; };
} }
@ -259,4 +279,6 @@ interface ExceptionExpectations {
void onTransactionExceptionOnPersistAndMergeAndFlush(RuntimeException e); void onTransactionExceptionOnPersistAndMergeAndFlush(RuntimeException e);
void onTransactionExceptionOnCommit(RuntimeException e); void onTransactionExceptionOnCommit(RuntimeException e);
void onExecuteUpdateWithConstraintViolation(RuntimeException e);
} }

View File

@ -91,6 +91,20 @@ public class QueryExceptionHandlingTest extends BaseExceptionHandlingTest {
} }
} }
@Test
@TestForIssue(jiraKey = "HHH-13300")
public void testExecuteUpdateWithConstraintViolation() {
try {
TransactionUtil2.inTransaction( sessionFactory(), s -> {
s.createQuery( "update A set id = 1 where id = 2" ).executeUpdate();
} );
fail( "should have thrown an exception" );
}
catch (RuntimeException expected) {
exceptionExpectations.onExecuteUpdateWithConstraintViolation( expected );
}
}
@Entity(name = "A") @Entity(name = "A")
public static class A { public static class A {
@Id @Id