1
+ /*
2
+ * Hibernate, Relational Persistence for Idiomatic Java
3
+ *
4
+ * License: GNU Lesser General Public License (LGPL), version 2.1 or later
5
+ * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6
+ */
7
+ package org .hibernate .orm .test .query .hql .instantiation ;
8
+
9
+ import org .hibernate .annotations .Imported ;
10
+ import org .hibernate .query .sqm .sql .internal .InstantiationException ;
11
+
12
+ import org .hibernate .testing .orm .junit .DomainModel ;
13
+ import org .hibernate .testing .orm .junit .Jira ;
14
+ import org .hibernate .testing .orm .junit .SessionFactory ;
15
+ import org .hibernate .testing .orm .junit .SessionFactoryScope ;
16
+ import org .junit .jupiter .api .BeforeAll ;
17
+ import org .junit .jupiter .api .Test ;
18
+
19
+ import jakarta .persistence .Entity ;
20
+ import jakarta .persistence .GeneratedValue ;
21
+ import jakarta .persistence .Id ;
22
+
23
+ import static org .assertj .core .api .Assertions .assertThat ;
24
+ import static org .assertj .core .api .Assertions .fail ;
25
+
26
+ /**
27
+ * @author Marco Belladelli
28
+ */
29
+ @ DomainModel ( annotatedClasses = {
30
+ InstantiationWithMultiplePrimitiveConstructorsTest .TestEntity .class ,
31
+ InstantiationWithMultiplePrimitiveConstructorsTest .TestDto .class ,
32
+ } )
33
+ @ SessionFactory
34
+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-17155" )
35
+ public class InstantiationWithMultiplePrimitiveConstructorsTest {
36
+ @ BeforeAll
37
+ public void setUp (SessionFactoryScope scope ) {
38
+ scope .inTransaction ( session -> session .persist ( new TestEntity (
39
+ Integer .MAX_VALUE ,
40
+ Long .MAX_VALUE ,
41
+ "test"
42
+ ) ) );
43
+ }
44
+
45
+ @ Test
46
+ public void testIntegerConstructor (SessionFactoryScope scope ) {
47
+ scope .inTransaction ( session -> {
48
+ final TestDto result = session .createQuery (
49
+ String .format ( "select new %s(t.intValue, t.name) from TestEntity t" , TestDto .class .getName () ),
50
+ TestDto .class
51
+ ).getSingleResult ();
52
+ assertThat ( result .getValue () ).isEqualTo ( String .valueOf ( Integer .MAX_VALUE ) );
53
+ } );
54
+ }
55
+
56
+ @ Test
57
+ public void testIntegerInjection (SessionFactoryScope scope ) {
58
+ scope .inTransaction ( session -> {
59
+ final TestInjection result = session .createQuery ( String .format (
60
+ "select new %s(t.intValue as value, t.name as title) from TestEntity t" ,
61
+ TestInjection .class .getName ()
62
+ ), TestInjection .class ).getSingleResult ();
63
+ assertThat ( result .getValue () ).isEqualTo ( Integer .MAX_VALUE );
64
+ } );
65
+ }
66
+
67
+ @ Test
68
+ public void testLongConstructor (SessionFactoryScope scope ) {
69
+ scope .inTransaction ( session -> {
70
+ final TestDto result = session .createQuery (
71
+ String .format ( "select new %s(t.longValue, t.name) from TestEntity t" , TestDto .class .getName () ),
72
+ TestDto .class
73
+ ).getSingleResult ();
74
+ assertThat ( result .getValue () ).isEqualTo ( String .valueOf ( Long .MAX_VALUE ) );
75
+ } );
76
+ }
77
+
78
+ @ Test
79
+ public void testLongInjection (SessionFactoryScope scope ) {
80
+ scope .inTransaction ( session -> {
81
+ try {
82
+ session .createQuery ( String .format (
83
+ "select new %s(t.longValue as value, t.name as title) from TestEntity t" ,
84
+ TestInjection .class .getName ()
85
+ ), TestInjection .class ).getSingleResult ();
86
+ fail ( "Long assignment to int should not be allowed" );
87
+ }
88
+ catch (Exception e ) {
89
+ assertThat ( e ).isInstanceOf ( InstantiationException .class );
90
+ assertThat ( e .getMessage () ).contains ( "Cannot set field 'value'" );
91
+ }
92
+ } );
93
+ }
94
+
95
+ @ Entity ( name = "TestEntity" )
96
+ public static class TestEntity {
97
+ @ Id
98
+ @ GeneratedValue
99
+ private Long id ;
100
+
101
+ private Integer intValue ;
102
+
103
+ private Long longValue ;
104
+
105
+ private String name ;
106
+
107
+ public TestEntity () {
108
+ }
109
+
110
+ public TestEntity (Integer intValue , Long longValue , String name ) {
111
+ this .intValue = intValue ;
112
+ this .longValue = longValue ;
113
+ this .name = name ;
114
+ }
115
+ }
116
+
117
+ @ Imported
118
+ public static class TestDto {
119
+ private String value ;
120
+ private String title ;
121
+
122
+ public TestDto (String value , String title ) {
123
+ this .value = value ;
124
+ this .title = title ;
125
+ }
126
+
127
+ public TestDto (long value , String title ) {
128
+ this ( String .valueOf ( value ), title );
129
+ }
130
+
131
+ public TestDto (int value , String title ) {
132
+ this ( String .valueOf ( value ), title );
133
+ }
134
+
135
+ public String getValue () {
136
+ return value ;
137
+ }
138
+
139
+ public String getTitle () {
140
+ return title ;
141
+ }
142
+ }
143
+
144
+ public static class TestInjection {
145
+ private int value ;
146
+ private String title ;
147
+
148
+ public void setValue (int value ) {
149
+ this .value = value ;
150
+ }
151
+
152
+ public int getValue () {
153
+ return value ;
154
+ }
155
+
156
+ public String getTitle () {
157
+ return title ;
158
+ }
159
+
160
+ public void setTitle (String title ) {
161
+ this .title = title ;
162
+ }
163
+ }
164
+ }
0 commit comments