13
13
14
14
import org .junit .Test ;
15
15
16
+ import org .hibernate .Hibernate ;
17
+ import org .hibernate .internal .util .collections .ArrayHelper ;
16
18
import org .hibernate .jpa .test .BaseEntityManagerFunctionalTestCase ;
17
19
import org .hibernate .jpa .test .Cat ;
18
20
import org .hibernate .jpa .test .Kitten ;
19
21
20
22
import org .hibernate .testing .FailureExpected ;
23
+ import org .hibernate .testing .TestForIssue ;
21
24
22
25
import static org .junit .Assert .assertEquals ;
23
26
import static org .junit .Assert .assertFalse ;
@@ -263,6 +266,7 @@ public Class[] getAnnotatedClasses() {
263
266
* @throws Exception
264
267
*/
265
268
@ Test
269
+ @ TestForIssue ( jiraKey = "HHH-7573" )
266
270
public void testJpaFlushEntityEventListener () throws Exception {
267
271
EntityWithLazyProperty entity ;
268
272
EntityManager em = getOrCreateEntityManager ();
@@ -276,51 +280,70 @@ public void testJpaFlushEntityEventListener() throws Exception {
276
280
entity .setLazyData (testArray );
277
281
em .persist (entity );
278
282
em .getTransaction ().commit ();
283
+ em .close ();
284
+
279
285
checkLazyField (entity , em , testArray );
280
286
281
287
/**
282
288
* Set a non lazy field, therefore the lazyData field will be LazyPropertyInitializer.UNFETCHED_PROPERTY
283
289
* for both state and newState so the field should not change. This should no longer cause a ClassCastException.
284
290
*/
291
+ em = getOrCreateEntityManager ();
285
292
em .getTransaction ().begin ();
286
293
entity = em .find (EntityWithLazyProperty .class , entity .getId ());
287
294
entity .setSomeField ("TEST1" );
295
+ assertFalse ( Hibernate .isPropertyInitialized ( entity , "lazyData" ) );
288
296
em .getTransaction ().commit ();
297
+ assertFalse ( Hibernate .isPropertyInitialized ( entity , "lazyData" ) );
298
+ em .close ();
299
+
289
300
checkLazyField (entity , em , testArray );
290
301
291
302
/**
292
303
* Set the updateLazyFieldInPreUpdate flag so that the lazy field is updated from within the
293
304
* PreUpdate annotated callback method. So state == LazyPropertyInitializer.UNFETCHED_PROPERTY and
294
305
* newState == EntityWithLazyProperty.PRE_UPDATE_VALUE. This should no longer cause a ClassCastException.
295
306
*/
307
+ em = getOrCreateEntityManager ();
296
308
em .getTransaction ().begin ();
297
309
entity = em .find (EntityWithLazyProperty .class , entity .getId ());
298
310
entity .setUpdateLazyFieldInPreUpdate (true );
299
311
entity .setSomeField ("TEST2" );
312
+ assertFalse ( Hibernate .isPropertyInitialized ( entity , "lazyData" ) );
300
313
em .getTransaction ().commit ();
314
+ assertTrue ( Hibernate .isPropertyInitialized ( entity , "lazyData" ) );
315
+ em .close ();
316
+
301
317
checkLazyField (entity , em , EntityWithLazyProperty .PRE_UPDATE_VALUE );
302
318
303
319
/**
304
320
* Set the updateLazyFieldInPreUpdate flag so that the lazy field is updated from within the
305
321
* PreUpdate annotated callback method and also set the lazyData field directly to testArray1. When we reload we
306
322
* should get EntityWithLazyProperty.PRE_UPDATE_VALUE.
307
323
*/
324
+ em = getOrCreateEntityManager ();
308
325
em .getTransaction ().begin ();
309
326
entity = em .find (EntityWithLazyProperty .class , entity .getId ());
310
327
entity .setUpdateLazyFieldInPreUpdate (true );
328
+ assertFalse ( Hibernate .isPropertyInitialized ( entity , "lazyData" ) );
311
329
entity .setLazyData (testArray );
330
+ assertTrue ( Hibernate .isPropertyInitialized ( entity , "lazyData" ) );
312
331
entity .setSomeField ("TEST3" );
313
332
em .getTransaction ().commit ();
314
- checkLazyField (entity , em , EntityWithLazyProperty .PRE_UPDATE_VALUE );
315
-
316
333
em .close ();
334
+
335
+ checkLazyField ( entity , em , EntityWithLazyProperty .PRE_UPDATE_VALUE );
317
336
}
318
337
319
338
private void checkLazyField (EntityWithLazyProperty entity , EntityManager em , byte [] expected ) {
320
339
// reload the entity and check the lazy value matches what we expect.
340
+ em = getOrCreateEntityManager ();
321
341
em .getTransaction ().begin ();
322
342
entity = em .find (EntityWithLazyProperty .class , entity .getId ());
323
- assertEquals (expected , entity .getLazyData ());
343
+ assertFalse ( Hibernate .isPropertyInitialized ( entity , "lazyData" ) );
344
+ assertTrue ( ArrayHelper .isEquals ( expected , entity .getLazyData () ) );
345
+ assertTrue ( Hibernate .isPropertyInitialized ( entity , "lazyData" ) );
324
346
em .getTransaction ().commit ();
347
+ em .close ();
325
348
}
326
349
}
0 commit comments