From 1979aed658c06b1650a4fb4ff49ac8c956098096 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 28 Aug 2023 16:48:21 -0500 Subject: [PATCH] HHH-17143 - Bad handling of not-found association references in mutation queries https://hibernate.atlassian.net/browse/HHH-17143 --- .../orm/test/associations/NotFoundTest.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/associations/NotFoundTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/associations/NotFoundTest.java index 946c5f4392..f46528c5fa 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/associations/NotFoundTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/associations/NotFoundTest.java @@ -14,6 +14,8 @@ import org.hibernate.annotations.NotFoundAction; import org.hibernate.testing.jdbc.SQLStatementInspector; import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.FailureExpected; +import org.hibernate.testing.orm.junit.Jira; import org.hibernate.testing.orm.junit.Jpa; import org.junit.jupiter.api.AfterEach; @@ -135,6 +137,38 @@ public class NotFoundTest { ); } + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-17143" ) + @FailureExpected + void testHqlDelete(EntityManagerFactoryScope scope) { + breakForeignKey( scope ); + final SQLStatementInspector sqlStatementInspector = scope.getCollectingStatementInspector(); + sqlStatementInspector.clear(); + + scope.inTransaction( (entityManager) -> { + entityManager.createQuery( "delete from Person p where p.city is null" ).executeUpdate(); + + assertThat( sqlStatementInspector.getSqlQueries() ).hasSize( 1 ); + assertThat( sqlStatementInspector.getSqlQueries().get( 0 ) ).doesNotContainIgnoringCase( "city_fk is null" ); + } ); + } + + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-17143" ) + @FailureExpected + void testHqlUpdate(EntityManagerFactoryScope scope) { + breakForeignKey( scope ); + final SQLStatementInspector sqlStatementInspector = scope.getCollectingStatementInspector(); + sqlStatementInspector.clear(); + + scope.inTransaction( (entityManager) -> { + entityManager.createQuery( "update Person p set p.name = 'abc' where p.city is null" ).executeUpdate(); + + assertThat( sqlStatementInspector.getSqlQueries() ).hasSize( 1 ); + assertThat( sqlStatementInspector.getSqlQueries().get( 0 ) ).doesNotContainIgnoringCase( "city_fk is null" ); + } ); + } + private void breakForeignKey(EntityManagerFactoryScope scope) { scope.inTransaction( entityManager -> {