From b7d9f4eaa193a79a3aab1f5df777973ba3e9c594 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Mon, 9 May 2022 14:46:57 +0200 Subject: [PATCH] HHH-15256 Add test for issue --- .../test/query/EntityAsParamValueTest.java | 55 ++++++++++++++++++- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/EntityAsParamValueTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/EntityAsParamValueTest.java index 6889b9a7a8..0093b0db83 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/EntityAsParamValueTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/EntityAsParamValueTest.java @@ -10,6 +10,7 @@ import org.hibernate.testing.TestForIssue; import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -19,6 +20,8 @@ import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertNotNull; @DomainModel( @@ -28,17 +31,19 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; } ) @SessionFactory -@TestForIssue(jiraKey = "HHH-15223") public class EntityAsParamValueTest { + public static long ID_ENTITY_WHITOUT_ORGANIZER = 1; + public static long ID_ENTITY_WHIT_ORGANIZER = 2; + @BeforeEach public void setUp(SessionFactoryScope scope) { scope.inTransaction( session -> { Organizer organizer = new Organizer( 1L, "Test Organizer" ); - Event eventWithOrganizer = new Event( 1L, "Test Event", organizer ); - Event eventWithoutOrganizer = new Event( 2L, "Null Event", null ); + Event eventWithOrganizer = new Event( ID_ENTITY_WHIT_ORGANIZER, "Test Event", organizer ); + Event eventWithoutOrganizer = new Event( ID_ENTITY_WHITOUT_ORGANIZER, "Null Event", null ); session.persist( organizer ); session.persist( eventWithOrganizer ); @@ -47,7 +52,49 @@ public class EntityAsParamValueTest { ); } + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createMutationQuery( "delete from Event" ).executeUpdate(); + session.createMutationQuery( "delete from Organizer" ).executeUpdate(); + } + ); + } + @Test + @TestForIssue(jiraKey = "HHH-15256") + public void testQueryWithLeftJoinEntityAsParamValue(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Organizer organizer = session.get( Organizer.class, 1L ); + assertNotNull( organizer ); + + Event event = session.createQuery( + "FROM Event e LEFT JOIN e.organizer WHERE (:organizer IS NULL AND e.organizer IS NULL OR e.organizer = :organizer)", + Event.class + ) + .setParameter( "organizer", organizer ) + .setMaxResults( 1 ) + .uniqueResult(); + assertNotNull( event ); + assertThat( event.getId(), is( ID_ENTITY_WHIT_ORGANIZER ) ); + + event = session.createQuery( + "FROM Event e LEFT JOIN e.organizer WHERE (:organizer IS NULL AND e.organizer IS NULL OR e.organizer = :organizer)", + Event.class + ) + .setParameter( "organizer", null ) + .setMaxResults( 1 ) + .uniqueResult(); + assertNotNull( event ); + assertThat( event.getId(), is( ID_ENTITY_WHITOUT_ORGANIZER ) ); + } + ); + } + + @Test + @TestForIssue(jiraKey = "HHH-15223") public void testQueryWithEntityAsParamValue(SessionFactoryScope scope) { scope.inTransaction( session -> { @@ -62,6 +109,7 @@ public class EntityAsParamValueTest { .setMaxResults( 1 ) .uniqueResult(); assertNotNull( event ); + assertThat( event.getId(), is( ID_ENTITY_WHIT_ORGANIZER ) ); event = session.createQuery( "FROM Event e WHERE (:organizer IS NULL AND e.organizer IS NULL OR e.organizer = :organizer)", @@ -71,6 +119,7 @@ public class EntityAsParamValueTest { .setMaxResults( 1 ) .uniqueResult(); assertNotNull( event ); + assertThat( event.getId(), is( ID_ENTITY_WHITOUT_ORGANIZER ) ); } ); }