From 01c824387653bbf8a3de4e6585bdcccfbcda6f9d Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Tue, 17 Oct 2023 12:38:59 +0200 Subject: [PATCH] HHH-17299 Add test for issue --- .../TreatedSubclassSameTypeTest.java | 148 ++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/TreatedSubclassSameTypeTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/TreatedSubclassSameTypeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/TreatedSubclassSameTypeTest.java new file mode 100644 index 0000000000..af2c6b54ab --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/TreatedSubclassSameTypeTest.java @@ -0,0 +1,148 @@ +/* + * 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.function.Function; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Marco Belladelli + */ +@DomainModel( annotatedClasses = { + TreatedSubclassSameTypeTest.MyEntity1.class, + TreatedSubclassSameTypeTest.MyEntity2.class, + TreatedSubclassSameTypeTest.MySubEntity1.class, +} ) +@SessionFactory +@Jira( "https://hibernate.atlassian.net/browse/HHH-17299" ) +public class TreatedSubclassSameTypeTest { + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { + session.persist( new MyEntity1() ); + final MyEntity2 entity2 = new MyEntity2( "entity2" ); + final MySubEntity1 entity1 = new MySubEntity1( entity2 ); + session.persist( entity2 ); + session.persist( entity1 ); + } ); + } + + @AfterAll + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { + session.createMutationQuery( "delete from MyEntity1" ).executeUpdate(); + session.createMutationQuery( "delete from MyEntity2" ).executeUpdate(); + } ); + } + + @Test + public void testJoinOnTreat(SessionFactoryScope scope) { + executeQuery( scope, criteria -> criteria.from( MyEntity1.class ), true ); + } + + @Test + public void testJoin(SessionFactoryScope scope) { + executeQuery( scope, criteria -> criteria.from( MyEntity1.class ), false ); + } + + @Test + public void testJoinOnTreatedSubtype(SessionFactoryScope scope) { + executeQuery( scope, criteria -> criteria.from( MySubEntity1.class ), true ); + } + + @Test + public void testJoinOnSubtype(SessionFactoryScope scope) { + executeQuery( scope, criteria -> criteria.from( MySubEntity1.class ), false ); + } + + @SuppressWarnings( "unchecked" ) + private void executeQuery( + SessionFactoryScope scope, + Function, Root> rootSupplier, + boolean treat) { + scope.inTransaction( session -> { + final CriteriaBuilder cb = session.getCriteriaBuilder(); + final CriteriaQuery criteria = cb.createQuery( MyEntity1.class ); + final Root root = rootSupplier.apply( criteria ); + final Root subRoot; + if ( treat ) { + subRoot = cb.treat( ( (Root) root ), MySubEntity1.class ); + } + else { + subRoot = (Root) root; + } + criteria.where( cb.equal( subRoot.join( "ref" ).get( "name" ), "entity2" ) ); + final MyEntity1 result = session.createQuery( criteria ).getSingleResult(); + assertThat( result ).isInstanceOf( MySubEntity1.class ); + assertThat( ( (MySubEntity1) result ).getRef().getName() ).isEqualTo( "entity2" ); + } ); + } + + @Entity( name = "MyEntity1" ) + @Inheritance( strategy = InheritanceType.SINGLE_TABLE ) + public static class MyEntity1 { + @Id + @GeneratedValue + private Long id; + } + + @Entity( name = "MySubEntity1" ) + public static class MySubEntity1 extends MyEntity1 { + @ManyToOne + private MyEntity2 ref; + + public MySubEntity1() { + } + + public MySubEntity1(MyEntity2 ref) { + this.ref = ref; + } + + public MyEntity2 getRef() { + return ref; + } + } + + @Entity( name = "MyEntity2" ) + public static class MyEntity2 { + @Id + @GeneratedValue + private Long id; + + private String name; + + public MyEntity2() { + } + + public MyEntity2(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } +}