1919
2020import static org .junit .jupiter .api .Assertions .assertEquals ;
2121
22- @ DomainModel (
23- annotatedClasses = { MatchingConstructorTest .TestEntity .class }
24- )
22+ @ DomainModel ( annotatedClasses = { DynamicInstantiationConstructorMatchingTest .TestEntity .class } )
2523@ SessionFactory
26- @ Jira ("https://hibernate.atlassian.net/browse/HHH-18322" )
27- public class MatchingConstructorTest {
28-
24+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-18322" )
25+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-18664" )
26+ public class DynamicInstantiationConstructorMatchingTest {
2927 @ BeforeAll
3028 public void prepareData (final SessionFactoryScope scope ) {
31- scope .inTransaction (
32- session -> session .persist ( new TestEntity ( 1 , 42 , "test" , 13 ) )
33- );
29+ scope .inTransaction ( session -> session .persist ( new TestEntity ( 1 , 42 , "test" , 13 ) ) );
3430 }
3531
3632 @ AfterAll
3733 public void cleanUpData (final SessionFactoryScope scope ) {
38- scope .inTransaction (
39- session -> session .createQuery ( "delete TestEntity" ).executeUpdate ()
40- );
34+ scope .getSessionFactory ().getSchemaManager ().truncateMappedObjects ();
35+ }
36+
37+ @ Test
38+ void testExplicitConstructor (final SessionFactoryScope scope ) {
39+ scope .inSession ( session -> {
40+ final var result = session .createQuery (
41+ "select new ConstructorDto(num, str) from TestEntity" ,
42+ ConstructorDto .class
43+ ).getSingleResult ();
44+ assertEquals ( 42 , result .getNum () );
45+ assertEquals ( "test" , result .getStr () );
46+ } );
4147 }
4248
4349 @ Test
4450 void testImplicitConstructor (final SessionFactoryScope scope ) {
51+ scope .inSession ( session -> {
52+ final var result = session .createQuery ( "select num, str from TestEntity" , ConstructorDto .class )
53+ .getSingleResult ();
54+ assertEquals ( 42 , result .getNum () );
55+ assertEquals ( "test" , result .getStr () );
56+ } );
57+ }
58+
59+ @ Test
60+ void testExplicitConstructorWithPrimitive (final SessionFactoryScope scope ) {
4561 scope .inSession ( session -> {
4662 final var result = session .createQuery (
47- "select num , str from TestEntity" ,
48- ConstructorDto .class
63+ "select new ConstructorWithPrimitiveDto(intValue , str) from TestEntity" ,
64+ ConstructorWithPrimitiveDto .class
4965 )
50- .setMaxResults ( 1 ). getSingleResult ();
51- assertEquals ( 42 , result .getNum () );
66+ .getSingleResult ();
67+ assertEquals ( 13 , result .getIntValue () );
5268 assertEquals ( "test" , result .getStr () );
5369 } );
5470 }
@@ -60,13 +76,13 @@ void testImplicitConstructorWithPrimitive(final SessionFactoryScope scope) {
6076 "select intValue, str from TestEntity" ,
6177 ConstructorWithPrimitiveDto .class
6278 )
63- .setMaxResults ( 1 ). getSingleResult ();
79+ .getSingleResult ();
6480 assertEquals ( 13 , result .getIntValue () );
6581 assertEquals ( "test" , result .getStr () );
6682 } );
6783 }
6884
69- @ Entity (name = "TestEntity" )
85+ @ Entity ( name = "TestEntity" )
7086 public static class TestEntity {
7187 @ Id
7288 private Integer id ;
@@ -86,38 +102,6 @@ public TestEntity(final Integer id, final Integer num, final String str, final i
86102 this .str = str ;
87103 this .intValue = intValue ;
88104 }
89-
90- public Integer getId () {
91- return id ;
92- }
93-
94- public void setId (final Integer id ) {
95- this .id = id ;
96- }
97-
98- public Integer getNum () {
99- return num ;
100- }
101-
102- public void setNum (final Integer num ) {
103- this .num = num ;
104- }
105-
106- public String getStr () {
107- return str ;
108- }
109-
110- public void setStr (final String str ) {
111- this .str = str ;
112- }
113-
114- public int getIntValue () {
115- return intValue ;
116- }
117-
118- public void setIntValue (final int intValue ) {
119- this .intValue = intValue ;
120- }
121105 }
122106
123107 @ Imported
0 commit comments