19
19
20
20
import static org .junit .jupiter .api .Assertions .assertEquals ;
21
21
22
- @ DomainModel (
23
- annotatedClasses = { MatchingConstructorTest .TestEntity .class }
24
- )
22
+ @ DomainModel ( annotatedClasses = { DynamicInstantiationConstructorMatchingTest .TestEntity .class } )
25
23
@ 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 {
29
27
@ BeforeAll
30
28
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 ) ) );
34
30
}
35
31
36
32
@ AfterAll
37
33
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
+ } );
41
47
}
42
48
43
49
@ Test
44
50
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 ) {
45
61
scope .inSession ( session -> {
46
62
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
49
65
)
50
- .setMaxResults ( 1 ). getSingleResult ();
51
- assertEquals ( 42 , result .getNum () );
66
+ .getSingleResult ();
67
+ assertEquals ( 13 , result .getIntValue () );
52
68
assertEquals ( "test" , result .getStr () );
53
69
} );
54
70
}
@@ -60,13 +76,13 @@ void testImplicitConstructorWithPrimitive(final SessionFactoryScope scope) {
60
76
"select intValue, str from TestEntity" ,
61
77
ConstructorWithPrimitiveDto .class
62
78
)
63
- .setMaxResults ( 1 ). getSingleResult ();
79
+ .getSingleResult ();
64
80
assertEquals ( 13 , result .getIntValue () );
65
81
assertEquals ( "test" , result .getStr () );
66
82
} );
67
83
}
68
84
69
- @ Entity (name = "TestEntity" )
85
+ @ Entity ( name = "TestEntity" )
70
86
public static class TestEntity {
71
87
@ Id
72
88
private Integer id ;
@@ -86,38 +102,6 @@ public TestEntity(final Integer id, final Integer num, final String str, final i
86
102
this .str = str ;
87
103
this .intValue = intValue ;
88
104
}
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
- }
121
105
}
122
106
123
107
@ Imported
0 commit comments