From 9da230a616125069ca799a457ee2ef8781956c85 Mon Sep 17 00:00:00 2001 From: Jan Schatteman Date: Wed, 22 May 2024 21:10:51 +0200 Subject: [PATCH] HHH-12885 - Add test Signed-off-by: Jan Schatteman --- .../BiDirectionalOneToOneFetchTest.java | 132 ++++++++++++++++-- 1 file changed, 122 insertions(+), 10 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/bidirectional/BiDirectionalOneToOneFetchTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/bidirectional/BiDirectionalOneToOneFetchTest.java index 50ffe98d48..68e66b1cde 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/bidirectional/BiDirectionalOneToOneFetchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/bidirectional/BiDirectionalOneToOneFetchTest.java @@ -30,12 +30,13 @@ import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToOne; import org.hibernate.engine.internal.StatisticalLoggingSessionEventListener; -import org.hibernate.testing.TestForIssue; import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.junit.jupiter.api.AfterEach; @@ -45,14 +46,15 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; /** - * Test cases for fetch joining a bi-directional one-to-one mapping. + * Test cases for fetch joining a bidirectional one-to-one mapping. * * @author Christian Beikov */ @DomainModel( annotatedClasses = { BiDirectionalOneToOneFetchTest.EntityA.class, - BiDirectionalOneToOneFetchTest.EntityB.class + BiDirectionalOneToOneFetchTest.EntityB.class, + BiDirectionalOneToOneFetchTest.EntityC.class } ) @SessionFactory @@ -63,14 +65,15 @@ public class BiDirectionalOneToOneFetchTest { scope.inTransaction( s -> { s.createQuery( "delete from EntityA" ).executeUpdate(); s.createQuery( "delete from EntityB" ).executeUpdate(); + s.createQuery( "delete from EntityC" ).executeUpdate(); } ); } @Test - @TestForIssue(jiraKey = "HHH-3930") + @JiraKey("HHH-3930") public void testEagerFetchBidirectionalOneToOneWithDirectFetching(SessionFactoryScope scope) { scope.inTransaction( session -> { - EntityA a = new EntityA( 1L, new EntityB( 2L ) ); + EntityA a = new EntityA( 1L, new EntityB( 2L ), new EntityC( 3L ) ); session.persist( a ); session.flush(); @@ -98,10 +101,10 @@ public class BiDirectionalOneToOneFetchTest { } @Test - @TestForIssue(jiraKey = "HHH-3930") + @JiraKey("HHH-3930") public void testFetchBidirectionalOneToOneWithOneJoinFetch(SessionFactoryScope scope) { scope.inTransaction( session -> { - EntityA a = new EntityA( 1L, new EntityB( 2L ) ); + EntityA a = new EntityA( 1L, new EntityB( 2L ), new EntityC( 3L ) ); session.persist( a ); session.flush(); @@ -135,10 +138,10 @@ public class BiDirectionalOneToOneFetchTest { } @Test - @TestForIssue(jiraKey = "HHH-3930") + @JiraKey("HHH-3930") public void testFetchBidirectionalOneToOneWithCircularJoinFetch(SessionFactoryScope scope) { scope.inTransaction( session -> { - EntityA a = new EntityA( 1L, new EntityB( 2L ) ); + EntityA a = new EntityA( 1L, new EntityB( 2L ), new EntityC( 3L ) ); session.persist( a ); session.flush(); @@ -166,6 +169,96 @@ public class BiDirectionalOneToOneFetchTest { } ); } + @Test + @JiraKey("HHH-12885") + public void testSelectInverseOneToOne(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + EntityA a = new EntityA( 1L, new EntityB( 2L ), new EntityC( 3L ) ); + + session.persist( a ); + session.flush(); + session.clear(); + + List tupleList = session.createQuery( + "select b, b.id, a from EntityB b left join b.a a", + Object[].class + ).list(); + + List list = session.createQuery( + "select a from EntityB b left join b.a a", + EntityA.class + ).list(); + + assertEquals( + "Selecting inverse one-to-one didn't construct the object properly from the result set!", + list.get( 0 ), + tupleList.get( 0 )[2] + ); + } + ); + } + + @Test + @JiraKey("HHH-12885") + public void testSelectInverseOneToOne2(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + EntityA a = new EntityA( 1L, new EntityB( 2L ), new EntityC( 3L ) ); + + session.persist( a ); + session.flush(); + session.clear(); + + List tupleList = session.createQuery( + "select b, b.id, a, a.id from EntityB b left join b.a a", + Object[].class + ).list(); + + List list = session.createQuery( + "select a from EntityB b left join b.a a", + EntityA.class + ).list(); + + assertEquals( + "Selecting inverse one-to-one didn't construct the object properly from the result set!", + list.get( 0 ), + tupleList.get( 0 )[2] + ); + } + ); + } + + @Test + @JiraKey("HHH-12885") + public void testSelectInverseOneToOne3(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + EntityA a = new EntityA( 1L, new EntityB( 2L ), new EntityC( 3L ) ); + + session.persist( a ); + session.flush(); + session.clear(); + + List tupleList = session.createQuery( + "select b, b.id, a, a.id from EntityB b left join b.a a left join fetch a.c", + Object[].class + ).list(); + + List list = session.createQuery( + "select a from EntityB b left join b.a a left join fetch a.c", + EntityA.class + ).list(); + + assertEquals( + "Selecting inverse one-to-one didn't construct the object properly from the result set!", + list.get( 0 ), + tupleList.get( 0 )[2] + ); + } + ); + } + @Entity(name = "EntityA") public static class EntityA { @@ -176,13 +269,18 @@ public class BiDirectionalOneToOneFetchTest { @JoinColumn(name = "b_id") private EntityB b; + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) + @JoinColumn(name = "c_id") + private EntityC c; + public EntityA() { } - public EntityA(Long id, EntityB b) { + public EntityA(Long id, EntityB b, EntityC c) { this.id = id; this.b = b; this.b.a = this; + this.c = c; } public EntityB getB() { @@ -215,4 +313,18 @@ public class BiDirectionalOneToOneFetchTest { } } + @Entity(name = "EntityC") + public static class EntityC { + + @Id + private Long id; + + public EntityC() { + } + + public EntityC(Long id) { + this.id = id; + } + } + }