From 09b5829e8e06a72bcda988f56bc7a42cb534552e Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Mon, 25 Mar 2024 14:27:53 +0100 Subject: [PATCH] HHH-17867 Add test for issue --- ...inedInheritanceToOneSameHierarchyTest.java | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/JoinedInheritanceToOneSameHierarchyTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/JoinedInheritanceToOneSameHierarchyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/JoinedInheritanceToOneSameHierarchyTest.java new file mode 100644 index 0000000000..98fcf3f189 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/JoinedInheritanceToOneSameHierarchyTest.java @@ -0,0 +1,160 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ + +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.inheritance; + +import java.util.List; + +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.BeforeAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.ManyToOne; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Marco Belladelli + */ +@DomainModel( annotatedClasses = { + JoinedInheritanceToOneSameHierarchyTest.MasterEntity.class, + JoinedInheritanceToOneSameHierarchyTest.ConfigEntity.class, + JoinedInheritanceToOneSameHierarchyTest.ContextEntity.class, + JoinedInheritanceToOneSameHierarchyTest.DocumentEntity.class, + JoinedInheritanceToOneSameHierarchyTest.TypeEntity.class, +} ) +@SessionFactory +public class JoinedInheritanceToOneSameHierarchyTest { + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final TypeEntity type = new TypeEntity(); + type.setId( 1L ); + type.setName( "type" ); + session.persist( type ); + + final ContextEntity context = new ContextEntity(); + context.setId( 2L ); + context.setType( type ); + session.persist( context ); + + final ConfigEntity config = new ConfigEntity(); + config.setId( 3L ); + session.persist( config ); + } ); + } + + @Test + public void testQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final List resultList = session.createQuery( + "from ConfigEntity c order by c.id", + ConfigEntity.class + ).getResultList(); + assertThat( resultList.get( 0 ) ).isInstanceOf( ContextEntity.class ); + assertThat( ( (ContextEntity) resultList.get( 0 ) ).getType().getName() ).isEqualTo( "type" ); + assertThat( resultList.get( 1 ) ).isInstanceOf( ConfigEntity.class ); + } ); + } + + @Test + public void testQueryAndJoin(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final List resultList = session.createQuery( + "from ConfigEntity c left join treat(c as ContextEntity).type order by c.id", + ConfigEntity.class + ).getResultList(); + assertThat( resultList.get( 0 ) ).isInstanceOf( ContextEntity.class ); + assertThat( ( (ContextEntity) resultList.get( 0 ) ).getType().getName() ).isEqualTo( "type" ); + assertThat( resultList.get( 1 ) ).isInstanceOf( ConfigEntity.class ); + } ); + } + + @Test + public void testQueryAndJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final List resultList = session.createQuery( + "from ConfigEntity c left join fetch treat(c as ContextEntity).type order by c.id", + ConfigEntity.class + ).getResultList(); + assertThat( resultList.get( 0 ) ).isInstanceOf( ContextEntity.class ); + assertThat( ( (ContextEntity) resultList.get( 0 ) ).getType().getName() ).isEqualTo( "type" ); + assertThat( resultList.get( 1 ) ).isInstanceOf( ConfigEntity.class ); + } ); + } + + @Test + public void testFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final ConfigEntity config = session.find( ConfigEntity.class, 2L ); + assertThat( config ).isInstanceOf( ContextEntity.class ); + assertThat( ( (ContextEntity) config ).getType().getName() ).isEqualTo( "type" ); + } ); + } + + @Entity( name = "MasterEntity" ) + @Inheritance( strategy = InheritanceType.JOINED ) + static class MasterEntity { + @Id + private Long id; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + } + + @Entity( name = "ConfigEntity" ) + static class ConfigEntity extends MasterEntity { + } + + @Entity( name = "ContextEntity" ) + static class ContextEntity extends ConfigEntity { + @ManyToOne + private TypeEntity type; + + public TypeEntity getType() { + return type; + } + + public void setType(TypeEntity type) { + this.type = type; + } + } + + @Entity( name = "DocumentEntity" ) + static class DocumentEntity extends MasterEntity { + } + + @Entity( name = "TypeEntity" ) + static class TypeEntity extends DocumentEntity { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +}