6
6
*/
7
7
package org .hibernate .test .bytecode .enhancement .lazy .basic ;
8
8
9
+ import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
10
+ import org .hibernate .cfg .AvailableSettings ;
11
+ import org .hibernate .cfg .Configuration ;
9
12
import org .hibernate .orm .test .bytecode .enhancement .lazy .NoDirtyCheckingContext ;
10
13
11
14
import static org .hibernate .testing .transaction .TransactionUtil .doInHibernate ;
16
19
import org .hibernate .testing .bytecode .enhancement .BytecodeEnhancerRunner ;
17
20
import org .hibernate .testing .bytecode .enhancement .CustomEnhancementContext ;
18
21
import org .hibernate .testing .bytecode .enhancement .EnhancerTestContext ;
22
+ import org .hibernate .testing .jdbc .SQLStatementInspector ;
19
23
import org .hibernate .testing .junit4 .BaseCoreFunctionalTestCase ;
24
+ import org .junit .Before ;
20
25
import org .junit .Test ;
21
26
import org .junit .runner .RunWith ;
22
27
29
34
30
35
@ RunWith (BytecodeEnhancerRunner .class )
31
36
@ CustomEnhancementContext ( {EnhancerTestContext .class , NoDirtyCheckingContext .class } )
32
- @ TestForIssue (jiraKey = "HHH-15634" )
37
+ @ TestForIssue (jiraKey = { "HHH-15634" , "HHH-16049" } )
33
38
public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase {
34
39
35
40
private Long entityId ;
@@ -39,6 +44,16 @@ public Class<?>[] getAnnotatedClasses() {
39
44
return new Class <?>[] { LazyEntity .class };
40
45
}
41
46
47
+ @ Override
48
+ protected void prepareBasicRegistryBuilder (StandardServiceRegistryBuilder serviceRegistryBuilder ) {
49
+ super .prepareBasicRegistryBuilder ( serviceRegistryBuilder );
50
+ serviceRegistryBuilder .applySetting ( AvailableSettings .STATEMENT_INSPECTOR , SQLStatementInspector .class .getName () );
51
+ }
52
+
53
+ SQLStatementInspector statementInspector () {
54
+ return (SQLStatementInspector ) sessionFactory ().getSessionFactoryOptions ().getStatementInspector ();
55
+ }
56
+
42
57
private void initNull () {
43
58
doInHibernate ( this ::sessionFactory , s -> {
44
59
LazyEntity entity = new LazyEntity ();
@@ -58,6 +73,23 @@ private void initNonNull() {
58
73
} );
59
74
}
60
75
76
+ @ Before
77
+ public void clearStatementInspector () {
78
+ statementInspector ().clear ();
79
+ }
80
+
81
+ @ Test
82
+ public void updateOneLazyProperty_nullToNull () {
83
+ initNull ();
84
+ doInHibernate ( this ::sessionFactory , s -> {
85
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
86
+ entity .setLazyProperty1 ( null );
87
+ } );
88
+
89
+ // We should not update entities when property values did not change
90
+ statementInspector ().assertNoUpdate ();
91
+ }
92
+
61
93
@ Test
62
94
public void updateOneLazyProperty_nullToNonNull () {
63
95
initNull ();
@@ -75,7 +107,7 @@ public void updateOneLazyProperty_nullToNonNull() {
75
107
}
76
108
77
109
@ Test
78
- public void updateOneLazyProperty_nonNullToNonNull () {
110
+ public void updateOneLazyProperty_nonNullToNonNull_differentValues () {
79
111
initNonNull ();
80
112
doInHibernate ( this ::sessionFactory , s -> {
81
113
LazyEntity entity = s .get ( LazyEntity .class , entityId );
@@ -90,6 +122,18 @@ public void updateOneLazyProperty_nonNullToNonNull() {
90
122
} );
91
123
}
92
124
125
+ @ Test
126
+ public void updateOneLazyProperty_nonNullToNonNull_sameValues () {
127
+ initNonNull ();
128
+ doInHibernate ( this ::sessionFactory , s -> {
129
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
130
+ entity .setLazyProperty1 ( "lazy1_update" );
131
+ } );
132
+
133
+ // We should not update entities when property values did not change
134
+ statementInspector ().assertNoUpdate ();
135
+ }
136
+
93
137
@ Test
94
138
public void updateOneLazyProperty_nonNullToNull () {
95
139
initNonNull ();
@@ -106,6 +150,91 @@ public void updateOneLazyProperty_nonNullToNull() {
106
150
} );
107
151
}
108
152
153
+ @ Test
154
+ public void updateOneEagerProperty_nullToNull () {
155
+ initNull ();
156
+ doInHibernate ( this ::sessionFactory , s -> {
157
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
158
+ entity .setEagerProperty ( null );
159
+ } );
160
+
161
+ // We should not update entities when property values did not change
162
+ statementInspector ().assertNoUpdate ();
163
+ }
164
+
165
+ @ Test
166
+ public void updateOneEagerProperty_nullToNonNull () {
167
+ initNull ();
168
+ doInHibernate ( this ::sessionFactory , s -> {
169
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
170
+ entity .setEagerProperty ( "eager_update" );
171
+ } );
172
+ doInHibernate ( this ::sessionFactory , s -> {
173
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
174
+ assertEquals ( "eager_update" , entity .getEagerProperty () );
175
+
176
+ assertNull ( entity .getLazyProperty1 () );
177
+ assertNull ( entity .getLazyProperty2 () );
178
+ } );
179
+ }
180
+
181
+ @ Test
182
+ public void updateOneEagerProperty_nonNullToNonNull_differentValues () {
183
+ initNonNull ();
184
+ doInHibernate ( this ::sessionFactory , s -> {
185
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
186
+ entity .setEagerProperty ( "eager_update" );
187
+ } );
188
+ doInHibernate ( this ::sessionFactory , s -> {
189
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
190
+ assertEquals ( "eager_update" , entity .getEagerProperty () );
191
+
192
+ assertEquals ( "lazy1_initial" , entity .getLazyProperty1 () );
193
+ assertEquals ( "lazy2_initial" , entity .getLazyProperty2 () );
194
+ } );
195
+ }
196
+
197
+ @ Test
198
+ public void updateOneEagerProperty_nonNullToNonNull_sameValues () {
199
+ initNonNull ();
200
+ doInHibernate ( this ::sessionFactory , s -> {
201
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
202
+ entity .setEagerProperty ( "eager_update" );
203
+ } );
204
+
205
+ // We should not update entities when property values did not change
206
+ statementInspector ().assertNoUpdate ();
207
+ }
208
+
209
+ @ Test
210
+ public void updateOneEagerProperty_nonNullToNull () {
211
+ initNonNull ();
212
+ doInHibernate ( this ::sessionFactory , s -> {
213
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
214
+ entity .setEagerProperty ( null );
215
+ } );
216
+ doInHibernate ( this ::sessionFactory , s -> {
217
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
218
+ assertNull ( entity .getEagerProperty () );
219
+
220
+ assertEquals ( "lazy1_initial" , entity .getLazyProperty1 () );
221
+ assertEquals ( "lazy2_initial" , entity .getLazyProperty2 () );
222
+ } );
223
+ }
224
+
225
+ @ Test
226
+ public void updateOneEagerPropertyAndOneLazyProperty_nullToNull () {
227
+ initNull ();
228
+ doInHibernate ( this ::sessionFactory , s -> {
229
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
230
+ entity .setEagerProperty ( null );
231
+ entity .setLazyProperty1 ( null );
232
+ } );
233
+
234
+ // We should not update entities when property values did not change
235
+ statementInspector ().assertNoUpdate ();
236
+ }
237
+
109
238
@ Test
110
239
public void updateOneEagerPropertyAndOneLazyProperty_nullToNonNull () {
111
240
initNull ();
@@ -124,7 +253,7 @@ public void updateOneEagerPropertyAndOneLazyProperty_nullToNonNull() {
124
253
}
125
254
126
255
@ Test
127
- public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull () {
256
+ public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull_differentValues () {
128
257
initNonNull ();
129
258
doInHibernate ( this ::sessionFactory , s -> {
130
259
LazyEntity entity = s .get ( LazyEntity .class , entityId );
@@ -140,6 +269,19 @@ public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull() {
140
269
} );
141
270
}
142
271
272
+ @ Test
273
+ public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull_sameValues () {
274
+ initNonNull ();
275
+ doInHibernate ( this ::sessionFactory , s -> {
276
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
277
+ entity .setEagerProperty ( entity .getEagerProperty () );
278
+ entity .setLazyProperty1 ( entity .getLazyProperty1 () );
279
+ } );
280
+
281
+ // We should not update entities when property values did not change
282
+ statementInspector ().assertNoUpdate ();
283
+ }
284
+
143
285
@ Test
144
286
public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNull () {
145
287
initNonNull ();
@@ -157,6 +299,19 @@ public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNull() {
157
299
} );
158
300
}
159
301
302
+ @ Test
303
+ public void updateAllLazyProperties_nullToNull () {
304
+ initNull ();
305
+ doInHibernate ( this ::sessionFactory , s -> {
306
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
307
+ entity .setLazyProperty1 ( null );
308
+ entity .setLazyProperty2 ( null );
309
+ } );
310
+
311
+ // We should not update entities when property values did not change
312
+ statementInspector ().assertNoUpdate ();
313
+ }
314
+
160
315
@ Test
161
316
public void updateAllLazyProperties_nullToNonNull () {
162
317
initNull ();
@@ -175,7 +330,7 @@ public void updateAllLazyProperties_nullToNonNull() {
175
330
}
176
331
177
332
@ Test
178
- public void updateAllLazyProperties_nonNullToNonNull () {
333
+ public void updateAllLazyProperties_nonNullToNonNull_differentValues () {
179
334
initNonNull ();
180
335
doInHibernate ( this ::sessionFactory , s -> {
181
336
LazyEntity entity = s .get ( LazyEntity .class , entityId );
@@ -191,6 +346,19 @@ public void updateAllLazyProperties_nonNullToNonNull() {
191
346
} );
192
347
}
193
348
349
+ @ Test
350
+ public void updateAllLazyProperties_nonNullToNonNull_sameValues () {
351
+ initNonNull ();
352
+ doInHibernate ( this ::sessionFactory , s -> {
353
+ LazyEntity entity = s .get ( LazyEntity .class , entityId );
354
+ entity .setLazyProperty1 ( entity .getLazyProperty1 () );
355
+ entity .setLazyProperty2 ( entity .getLazyProperty2 () );
356
+ } );
357
+
358
+ // We should not update entities when property values did not change
359
+ statementInspector ().assertNoUpdate ();
360
+ }
361
+
194
362
@ Test
195
363
public void updateAllLazyProperties_nonNullToNull () {
196
364
initNonNull ();
0 commit comments