From ecf4c29a608acbaaf18621a8b3b83efda096a916 Mon Sep 17 00:00:00 2001 From: marko-bekhta Date: Mon, 19 Aug 2024 17:31:29 +0200 Subject: [PATCH] HHH-18503 Add a test showing the issue --- .../join/JoinedHierarchyDiscIdSelectTest.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/join/JoinedHierarchyDiscIdSelectTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/join/JoinedHierarchyDiscIdSelectTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/join/JoinedHierarchyDiscIdSelectTest.java new file mode 100644 index 0000000000..d7e7dc3b8a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/join/JoinedHierarchyDiscIdSelectTest.java @@ -0,0 +1,106 @@ +/* + * 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.join; + +import java.io.Serializable; +import java.util.List; + +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.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import jakarta.persistence.DiscriminatorColumn; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; + +import static org.assertj.core.api.Assertions.assertThat; + +@DomainModel( annotatedClasses = { + JoinedHierarchyDiscIdSelectTest.DooredVehicle.class, + JoinedHierarchyDiscIdSelectTest.BaseVehicle.class, + JoinedHierarchyDiscIdSelectTest.BaseEntity.class, +} ) +@SessionFactory +@Jira( "https://hibernate.atlassian.net/browse/HHH-18503" ) +public class JoinedHierarchyDiscIdSelectTest { + @ParameterizedTest + @ValueSource( classes = { + DooredVehicle.class, + BaseVehicle.class, + BaseEntity.class, + } ) + void testSelectCriteriaId(Class klass, SessionFactoryScope scope) { + scope.inTransaction( session -> { + final CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + final CriteriaQuery criteriaQuery = criteriaBuilder.createQuery( Long.class ); + final Root root = criteriaQuery.from( klass ); + criteriaQuery.select( root.get( "id" ) ); + final List resultList = session.createQuery( criteriaQuery ).getResultList(); + assertThat( resultList ).hasSize( 1 ).containsOnly( 1L ); + } ); + } + + @ParameterizedTest + @ValueSource( classes = { + DooredVehicle.class, + BaseVehicle.class, + BaseEntity.class, + } ) + void testSelectId(Class klass, SessionFactoryScope scope) { + scope.inTransaction( session -> { + final Long result = session.createQuery( + String.format( "select id from %s", klass.getSimpleName() ), + Long.class + ).getSingleResult(); + assertThat( result ).isEqualTo( 1L ); + } ); + } + + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> session.persist( DooredVehicle.create( 1L ) ) ); + } + + @AfterAll + public void tearDown(SessionFactoryScope scope) { + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); + } + + @Entity( name = "DooredVehicle" ) + static class DooredVehicle extends BaseVehicle { + public String doorType; + + public static DooredVehicle create(Long id) { + DooredVehicle vehicle = new DooredVehicle(); + vehicle.id = id; + return vehicle; + } + } + + @Entity( name = "BaseVehicle" ) + static class BaseVehicle extends BaseEntity { + public String bodyType; + } + + @Entity( name = "BaseEntity" ) + @DiscriminatorColumn( name = "type" ) + @Inheritance( strategy = InheritanceType.JOINED ) + static class BaseEntity implements Serializable { + @Id + public Long id; + } +}