From 459c6f427ae8b841b2a467a5ec02ca0db6584bda Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Fri, 3 Nov 2023 17:25:23 +0100 Subject: [PATCH] HHH-17385 Missing table for superclass when joining subclass association --- .../TreatJoinOptimizationTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/TreatJoinOptimizationTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/TreatJoinOptimizationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/TreatJoinOptimizationTest.java new file mode 100644 index 0000000000..d4043068ad --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/TreatJoinOptimizationTest.java @@ -0,0 +1,58 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.inheritance; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; +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.Test; + +import java.util.HashSet; +import java.util.Set; + +@DomainModel( annotatedClasses = { + TreatJoinOptimizationTest.JoinedBase.class, + TreatJoinOptimizationTest.JoinedSub1.class +} ) +@SessionFactory +@JiraKey( "HHH-17385" ) +public class TreatJoinOptimizationTest { + + @Test + public void testSelectSuperclassAttributeOfJoinedSubclassCollection(SessionFactoryScope scope) { + scope.inTransaction( session -> { + session.createSelectionQuery( + "SELECT s1.value FROM JoinedBase b JOIN b.children s1", + Integer.class + ).getResultList(); + } ); + } + + @Entity( name = "JoinedBase" ) + @Inheritance( strategy = InheritanceType.JOINED ) + public static abstract class JoinedBase { + @Id + private Long id; + @Column(name = "val") + private Integer value; + } + + @Entity( name = "JoinedSub1" ) + @Table( name = "joined_sub_1" ) + public static class JoinedSub1 extends JoinedBase { + @OneToMany + private Set children = new HashSet<>(); + } +}