diff --git a/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/ExceptionExpectations.java b/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/ExceptionExpectations.java index e78d850c24..7c8dbc6081 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/ExceptionExpectations.java +++ b/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/ExceptionExpectations.java @@ -94,6 +94,13 @@ interface ExceptionExpectations { assertThat( e.getCause(), instanceOf( PersistenceException.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) { 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.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 onTransactionExceptionOnCommit(RuntimeException e); + + void onExecuteUpdateWithConstraintViolation(RuntimeException e); } \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/QueryExceptionHandlingTest.java b/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/QueryExceptionHandlingTest.java index ee38985c15..d408229fe7 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/QueryExceptionHandlingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/QueryExceptionHandlingTest.java @@ -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") public static class A { @Id