2020import org .hibernate .engine .spi .EntityKey ;
2121import org .hibernate .engine .spi .ManagedEntity ;
2222import org .hibernate .engine .spi .PersistenceContext ;
23+ import org .hibernate .engine .spi .PersistentAttributeInterceptable ;
2324import org .hibernate .engine .spi .SelfDirtinessTracker ;
2425import org .hibernate .engine .spi .SessionImplementor ;
2526import org .hibernate .engine .spi .SharedSessionContractImplementor ;
2627import org .hibernate .engine .spi .Status ;
2728import org .hibernate .internal .util .ImmutableBitSet ;
2829import org .hibernate .persister .entity .EntityPersister ;
30+ import org .hibernate .proxy .LazyInitializer ;
2931import org .hibernate .type .TypeHelper ;
3032
3133import org .checkerframework .checker .nullness .qual .Nullable ;
3739import static org .hibernate .engine .internal .EntityEntryImpl .EnumState .PREVIOUS_STATUS ;
3840import static org .hibernate .engine .internal .EntityEntryImpl .EnumState .STATUS ;
3941import 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 ;
4143import 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 ;
4444import static org .hibernate .engine .internal .ManagedTypeHelper .isSelfDirtinessTracker ;
4545import static org .hibernate .engine .internal .ManagedTypeHelper .processIfManagedEntity ;
4646import static org .hibernate .engine .internal .ManagedTypeHelper .processIfSelfDirtinessTracker ;
@@ -385,9 +385,17 @@ private boolean isUnequivocallyNonDirty(Object entity) {
385385 }
386386
387387 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 () ) {
391399 // we never have to check an uninitialized proxy
392400 return true ;
393401 }
@@ -400,28 +408,24 @@ private boolean isNonDirtyViaCustomStrategy(Object entity) {
400408 }
401409
402410 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 ();
408415 }
409- else {
410- uninitializedProxy = false ;
411- }
412- }
413- else if ( isHibernateProxy ( entity ) ) {
414- uninitializedProxy = extractLazyInitializer ( entity ).isUninitialized ();
415416 }
416417 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+ }
418423 }
419424 // 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 ();
425429 }
426430
427431 @ Override
0 commit comments