From cd60710c923b697b150366f9895a15f01042174e Mon Sep 17 00:00:00 2001 From: marko-bekhta Date: Wed, 24 Apr 2024 17:12:33 +0200 Subject: [PATCH] HHH-18007 Add a testcase for the issue --- .../MappedSuperclassWithGenericsTest.java | 103 +++++++++++++++--- 1 file changed, 87 insertions(+), 16 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/MappedSuperclassWithGenericsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/MappedSuperclassWithGenericsTest.java index 8a7087a509..9ebed2c128 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/MappedSuperclassWithGenericsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/MappedSuperclassWithGenericsTest.java @@ -7,44 +7,90 @@ package org.hibernate.orm.test.mapping; import java.io.Serializable; +import java.util.List; import java.util.Objects; + +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.Column; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.IdClass; import jakarta.persistence.MappedSuperclass; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Path; +import jakarta.persistence.criteria.Root; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.orm.junit.DomainModel; -import org.hibernate.testing.orm.junit.SessionFactory; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; -@TestForIssue(jiraKey = "HHH-14499") @DomainModel( annotatedClasses = { MappedSuperclassWithGenericsTest.IntermediateAbstractMapped.class, MappedSuperclassWithGenericsTest.BaseEntity.class, MappedSuperclassWithGenericsTest.AbstractGenericMappedSuperType.class, + MappedSuperclassWithGenericsTest.SimpleEntity.class, + MappedSuperclassWithGenericsTest.GenericIdBaseEntity.class } ) @SessionFactory public class MappedSuperclassWithGenericsTest { - @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-14499" ) public void testIt() { } - @MappedSuperclass - public static abstract class AbstractGenericMappedSuperType { + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-18007" ) + void testSelectCriteriaGenericId(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + final CriteriaQuery criteriaQuery = criteriaBuilder.createQuery( Long.class ); + final Root root = criteriaQuery.from( SimpleEntity.class ); + final Path idPath = root.get( "id" ); + criteriaQuery.select( idPath ); + final List resultList = session.createQuery( criteriaQuery ).getResultList(); + assertThat( resultList ).hasSize( 1 ).containsOnly( 1L ); + } ); + } - private T whateverType; + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-18007" ) + void testSelectGenericId(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final List resultList = session.createQuery( + "select e.id from SimpleEntity e", + Long.class + ).getResultList(); + assertThat( resultList ).hasSize( 1 ).containsOnly( 1L ); + } ); + } + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> session.persist( new SimpleEntity( 1L, "simple_1" ) ) ); + } + + @AfterAll + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createMutationQuery( "delete from SimpleEntity" ).executeUpdate() ); } @MappedSuperclass - @IdClass(PK.class) - public static abstract class IntermediateAbstractMapped extends AbstractGenericMappedSuperType { + public static abstract class AbstractGenericMappedSuperType { + private T whateverType; + } + @MappedSuperclass + @IdClass( PK.class ) + public static abstract class IntermediateAbstractMapped extends AbstractGenericMappedSuperType { @Id private String keyOne; @Id @@ -53,9 +99,8 @@ public class MappedSuperclassWithGenericsTest { private String keyThree; } - @SuppressWarnings("UnusedDeclaration") + @SuppressWarnings( "UnusedDeclaration" ) public static class PK implements Serializable { - private String keyOne; private String keyTwo; private String keyThree; @@ -80,11 +125,37 @@ public class MappedSuperclassWithGenericsTest { } } - @Entity(name = "BaseEntity") + @Entity( name = "BaseEntity" ) public static class BaseEntity extends IntermediateAbstractMapped { - String aString; - } + @MappedSuperclass + public static class GenericIdBaseEntity { + @Id + private T id; + + protected GenericIdBaseEntity(T id) { + this.id = id; + } + + public T getId() { + return id; + } + } + + @Entity( name = "SimpleEntity" ) + public static class SimpleEntity extends GenericIdBaseEntity { + @Column + private String string; + + public SimpleEntity() { + super( null ); + } + + protected SimpleEntity(Long id, String string) { + super( id ); + this.string = string; + } + } }