20
20
import org .hibernate .engine .spi .EntityKey ;
21
21
import org .hibernate .engine .spi .ManagedEntity ;
22
22
import org .hibernate .engine .spi .PersistenceContext ;
23
+ import org .hibernate .engine .spi .PersistentAttributeInterceptable ;
23
24
import org .hibernate .engine .spi .SelfDirtinessTracker ;
24
25
import org .hibernate .engine .spi .SessionImplementor ;
25
26
import org .hibernate .engine .spi .SharedSessionContractImplementor ;
26
27
import org .hibernate .engine .spi .Status ;
27
28
import org .hibernate .internal .util .ImmutableBitSet ;
28
29
import org .hibernate .persister .entity .EntityPersister ;
30
+ import org .hibernate .proxy .LazyInitializer ;
29
31
import org .hibernate .type .TypeHelper ;
30
32
31
33
import org .checkerframework .checker .nullness .qual .Nullable ;
37
39
import static org .hibernate .engine .internal .EntityEntryImpl .EnumState .PREVIOUS_STATUS ;
38
40
import static org .hibernate .engine .internal .EntityEntryImpl .EnumState .STATUS ;
39
41
import static org .hibernate .engine .internal .ManagedTypeHelper .asManagedEntity ;
40
- import static org .hibernate .engine .internal .ManagedTypeHelper .asPersistentAttributeInterceptable ;
42
+ import static org .hibernate .engine .internal .ManagedTypeHelper .asPersistentAttributeInterceptableOrNull ;
41
43
import static org .hibernate .engine .internal .ManagedTypeHelper .asSelfDirtinessTracker ;
42
- import static org .hibernate .engine .internal .ManagedTypeHelper .isHibernateProxy ;
43
- import static org .hibernate .engine .internal .ManagedTypeHelper .isPersistentAttributeInterceptable ;
44
44
import static org .hibernate .engine .internal .ManagedTypeHelper .isSelfDirtinessTracker ;
45
45
import static org .hibernate .engine .internal .ManagedTypeHelper .processIfManagedEntity ;
46
46
import static org .hibernate .engine .internal .ManagedTypeHelper .processIfSelfDirtinessTracker ;
@@ -385,9 +385,17 @@ private boolean isUnequivocallyNonDirty(Object entity) {
385
385
}
386
386
387
387
private boolean isNonDirtyViaCustomStrategy (Object entity ) {
388
- if ( isPersistentAttributeInterceptable ( entity ) ) {
389
- if ( asPersistentAttributeInterceptable ( entity ).$$_hibernate_getInterceptor ()
390
- instanceof EnhancementAsProxyLazinessInterceptor ) {
388
+ final PersistentAttributeInterceptable interceptable = asPersistentAttributeInterceptableOrNull ( entity );
389
+ if ( interceptable != null ) {
390
+ if ( interceptable .$$_hibernate_getInterceptor () instanceof EnhancementAsProxyLazinessInterceptor interceptor
391
+ && !interceptor .isInitialized () ) {
392
+ // we never have to check an uninitialized proxy
393
+ return true ;
394
+ }
395
+ }
396
+ else {
397
+ final LazyInitializer lazyInitializer = extractLazyInitializer ( entity );
398
+ if ( lazyInitializer != null && lazyInitializer .isUninitialized () ) {
391
399
// we never have to check an uninitialized proxy
392
400
return true ;
393
401
}
@@ -400,28 +408,24 @@ private boolean isNonDirtyViaCustomStrategy(Object entity) {
400
408
}
401
409
402
410
private boolean isNonDirtyViaTracker (Object entity ) {
403
- final boolean uninitializedProxy ;
404
- if ( isPersistentAttributeInterceptable ( entity ) ) {
405
- if ( asPersistentAttributeInterceptable ( entity ).$$_hibernate_getInterceptor ()
406
- instanceof EnhancementAsProxyLazinessInterceptor lazinessInterceptor ) {
407
- return !lazinessInterceptor .hasWrittenFieldNames ();
411
+ final PersistentAttributeInterceptable interceptable = asPersistentAttributeInterceptableOrNull ( entity );
412
+ if ( interceptable != null ) {
413
+ if ( interceptable .$$_hibernate_getInterceptor () instanceof EnhancementAsProxyLazinessInterceptor interceptor ) {
414
+ return !interceptor .hasWrittenFieldNames ();
408
415
}
409
- else {
410
- uninitializedProxy = false ;
411
- }
412
- }
413
- else if ( isHibernateProxy ( entity ) ) {
414
- uninitializedProxy = extractLazyInitializer ( entity ).isUninitialized ();
415
416
}
416
417
else {
417
- uninitializedProxy = false ;
418
+ final LazyInitializer lazyInitializer = extractLazyInitializer ( entity );
419
+ if ( lazyInitializer != null && lazyInitializer .isUninitialized () ) {
420
+ // we never have to check an uninitialized proxy
421
+ return true ;
422
+ }
418
423
}
419
424
// we never have to check an uninitialized proxy
420
- return uninitializedProxy
421
- || !persister .hasCollections ()
422
- && !persister .hasMutableProperties ()
423
- && !asSelfDirtinessTracker ( entity ).$$_hibernate_hasDirtyAttributes ()
424
- && asManagedEntity ( entity ).$$_hibernate_useTracker ();
425
+ return !persister .hasCollections ()
426
+ && !persister .hasMutableProperties ()
427
+ && asManagedEntity ( entity ).$$_hibernate_useTracker ()
428
+ && !asSelfDirtinessTracker ( entity ).$$_hibernate_hasDirtyAttributes ();
425
429
}
426
430
427
431
@ Override
0 commit comments