HHH-18664 Add test for issue

This commit is contained in:
Marco Belladelli 2024-09-26 11:56:59 +02:00
parent eb03db7401
commit fc38d88930
1 changed files with 35 additions and 51 deletions

View File

@ -19,40 +19,56 @@ import jakarta.persistence.Id;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@DomainModel( @DomainModel( annotatedClasses = { DynamicInstantiationConstructorMatchingTest.TestEntity.class } )
annotatedClasses = { MatchingConstructorTest.TestEntity.class }
)
@SessionFactory @SessionFactory
@Jira("https://hibernate.atlassian.net/browse/HHH-18322") @Jira( "https://hibernate.atlassian.net/browse/HHH-18322" )
public class MatchingConstructorTest { @Jira( "https://hibernate.atlassian.net/browse/HHH-18664" )
public class DynamicInstantiationConstructorMatchingTest {
@BeforeAll @BeforeAll
public void prepareData(final SessionFactoryScope scope) { public void prepareData(final SessionFactoryScope scope) {
scope.inTransaction( scope.inTransaction( session -> session.persist( new TestEntity( 1, 42, "test", 13 ) ) );
session -> session.persist( new TestEntity( 1, 42, "test", 13 ) )
);
} }
@AfterAll @AfterAll
public void cleanUpData(final SessionFactoryScope scope) { public void cleanUpData(final SessionFactoryScope scope) {
scope.inTransaction( scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
session -> session.createQuery( "delete TestEntity" ).executeUpdate() }
);
@Test
void testExplicitConstructor(final SessionFactoryScope scope) {
scope.inSession( session -> {
final var result = session.createQuery(
"select new ConstructorDto(num, str) from TestEntity",
ConstructorDto.class
).getSingleResult();
assertEquals( 42, result.getNum() );
assertEquals( "test", result.getStr() );
} );
} }
@Test @Test
void testImplicitConstructor(final SessionFactoryScope scope) { void testImplicitConstructor(final SessionFactoryScope scope) {
scope.inSession( session -> { scope.inSession( session -> {
final var result = session.createQuery( final var result = session.createQuery( "select num, str from TestEntity", ConstructorDto.class )
"select num, str from TestEntity", .getSingleResult();
ConstructorDto.class
)
.setMaxResults( 1 ).getSingleResult();
assertEquals( 42, result.getNum() ); assertEquals( 42, result.getNum() );
assertEquals( "test", result.getStr() ); assertEquals( "test", result.getStr() );
} ); } );
} }
@Test
void testExplicitConstructorWithPrimitive(final SessionFactoryScope scope) {
scope.inSession( session -> {
final var result = session.createQuery(
"select new ConstructorWithPrimitiveDto(intValue, str) from TestEntity",
ConstructorWithPrimitiveDto.class
)
.getSingleResult();
assertEquals( 13, result.getIntValue() );
assertEquals( "test", result.getStr() );
} );
}
@Test @Test
void testImplicitConstructorWithPrimitive(final SessionFactoryScope scope) { void testImplicitConstructorWithPrimitive(final SessionFactoryScope scope) {
scope.inSession( session -> { scope.inSession( session -> {
@ -60,13 +76,13 @@ public class MatchingConstructorTest {
"select intValue, str from TestEntity", "select intValue, str from TestEntity",
ConstructorWithPrimitiveDto.class ConstructorWithPrimitiveDto.class
) )
.setMaxResults( 1 ).getSingleResult(); .getSingleResult();
assertEquals( 13, result.getIntValue() ); assertEquals( 13, result.getIntValue() );
assertEquals( "test", result.getStr() ); assertEquals( "test", result.getStr() );
} ); } );
} }
@Entity(name = "TestEntity") @Entity( name = "TestEntity" )
public static class TestEntity { public static class TestEntity {
@Id @Id
private Integer id; private Integer id;
@ -86,38 +102,6 @@ public class MatchingConstructorTest {
this.str = str; this.str = str;
this.intValue = intValue; this.intValue = intValue;
} }
public Integer getId() {
return id;
}
public void setId(final Integer id) {
this.id = id;
}
public Integer getNum() {
return num;
}
public void setNum(final Integer num) {
this.num = num;
}
public String getStr() {
return str;
}
public void setStr(final String str) {
this.str = str;
}
public int getIntValue() {
return intValue;
}
public void setIntValue(final int intValue) {
this.intValue = intValue;
}
} }
@Imported @Imported