diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/generics/TypeVariableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/generics/TypeVariableTest.java new file mode 100644 index 0000000000..17463cfdd3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/generics/TypeVariableTest.java @@ -0,0 +1,141 @@ +package org.hibernate.orm.test.annotations.generics; + +import org.hibernate.query.Query; + +import org.hibernate.testing.TestForIssue; +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.BeforeEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +@DomainModel( + annotatedClasses = { + TypeVariableTest.SimpleEntity.class, + }, + extraQueryImportClasses = { + TypeVariableTest.BasicSetterBasedDto.class + } +) +@SessionFactory +@TestForIssue(jiraKey = "HHH-15646") +public class TypeVariableTest { + + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + SimpleEntity simpleEntity = new SimpleEntity( 1, "Fab", Gender.FEMALE ); + session.persist( simpleEntity ); + } + ); + } + + @Test + public void testHqlBasicSetterDynamicInstantiation(SessionFactoryScope scope) { + scope.getSessionFactory().inTransaction( + session -> { + final Query query = session.createQuery( + "select new BasicSetterBasedDto( e.gender as gender, e.name as value ) from SimpleEntity e", + BasicSetterBasedDto.class + ); + + final BasicSetterBasedDto dto = query.getSingleResult(); + assertThat( dto, notNullValue() ); + + assertThat( dto.gender, is( Gender.FEMALE ) ); + assertThat( dto.value, is( "Fab" ) ); + } + ); + } + + public static class BasicSetterBasedDto { + private E gender; + private T value; + + public BasicSetterBasedDto() { + } + + public BasicSetterBasedDto(E gender, T value) { + this.gender = gender; + this.value = value; + } + + public E getGender() { + return gender; + } + + public void setGender(E gender) { + this.gender = gender; + } + + public T getValue() { + return value; + } + + public void setValue(T value) { + this.value = value; + } + } + + @Entity(name = "SimpleEntity") + @Table(name = "mapping_simple_entity") + public static class SimpleEntity { + private Integer id; + private String name; + private Gender gender; + + public SimpleEntity() { + } + + public SimpleEntity(Integer id, String name, Gender gender) { + this.id = id; + this.name = name; + this.gender = gender; + } + + @Id + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + + @Enumerated(EnumType.STRING) + public Gender getGender() { + return gender; + } + + public void setGender(Gender gender) { + this.gender = gender; + } + + } + + public enum Gender { + MALE, + FEMALE + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/generics/WildcardsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/generics/WildcardsTest.java index 3d57a95104..9968376297 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/generics/WildcardsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/generics/WildcardsTest.java @@ -13,6 +13,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import jakarta.persistence.AttributeConverter; +import jakarta.persistence.Column; import jakarta.persistence.Convert; import jakarta.persistence.Converter; import jakarta.persistence.Entity; @@ -68,6 +69,7 @@ public class WildcardsTest { BigDecimal balance; @Convert(converter = BalanceUsageConverter.class) + @Column(name = "account_usage") Set usage; public BankAccount() {