3737import static org .hibernate .engine .internal .EntityEntryImpl .EnumState .PREVIOUS_STATUS ;
3838import static org .hibernate .engine .internal .EntityEntryImpl .EnumState .STATUS ;
3939import static org .hibernate .engine .internal .ManagedTypeHelper .asManagedEntity ;
40- import static org .hibernate .engine .internal .ManagedTypeHelper .asPersistentAttributeInterceptable ;
40+ import static org .hibernate .engine .internal .ManagedTypeHelper .asPersistentAttributeInterceptableOrNull ;
4141import 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 ;
4442import static org .hibernate .engine .internal .ManagedTypeHelper .isSelfDirtinessTracker ;
4543import static org .hibernate .engine .internal .ManagedTypeHelper .processIfManagedEntity ;
4644import static org .hibernate .engine .internal .ManagedTypeHelper .processIfSelfDirtinessTracker ;
@@ -385,9 +383,17 @@ private boolean isUnequivocallyNonDirty(Object entity) {
385383 }
386384
387385 private boolean isNonDirtyViaCustomStrategy (Object entity ) {
388- if ( isPersistentAttributeInterceptable ( entity ) ) {
389- if ( asPersistentAttributeInterceptable ( entity ).$$_hibernate_getInterceptor ()
390- instanceof EnhancementAsProxyLazinessInterceptor ) {
386+ final var interceptable = asPersistentAttributeInterceptableOrNull ( entity );
387+ if ( interceptable != null ) {
388+ if ( interceptable .$$_hibernate_getInterceptor () instanceof EnhancementAsProxyLazinessInterceptor interceptor
389+ && !interceptor .isInitialized () ) {
390+ // we never have to check an uninitialized proxy
391+ return true ;
392+ }
393+ }
394+ else {
395+ final var lazyInitializer = extractLazyInitializer ( entity );
396+ if ( lazyInitializer != null && lazyInitializer .isUninitialized () ) {
391397 // we never have to check an uninitialized proxy
392398 return true ;
393399 }
@@ -400,28 +406,24 @@ private boolean isNonDirtyViaCustomStrategy(Object entity) {
400406 }
401407
402408 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 ();
409+ final var interceptable = asPersistentAttributeInterceptableOrNull ( entity );
410+ if ( interceptable != null ) {
411+ if ( interceptable .$$_hibernate_getInterceptor () instanceof EnhancementAsProxyLazinessInterceptor interceptor ) {
412+ return !interceptor .hasWrittenFieldNames ();
408413 }
409- else {
410- uninitializedProxy = false ;
411- }
412- }
413- else if ( isHibernateProxy ( entity ) ) {
414- uninitializedProxy = extractLazyInitializer ( entity ).isUninitialized ();
415414 }
416415 else {
417- uninitializedProxy = false ;
416+ final var lazyInitializer = extractLazyInitializer ( entity );
417+ if ( lazyInitializer != null && lazyInitializer .isUninitialized () ) {
418+ // we never have to check an uninitialized proxy
419+ return true ;
420+ }
418421 }
419422 // 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 ();
423+ return !persister .hasCollections ()
424+ && !persister .hasMutableProperties ()
425+ && asManagedEntity ( entity ).$$_hibernate_useTracker ()
426+ && !asSelfDirtinessTracker ( entity ).$$_hibernate_hasDirtyAttributes ();
425427 }
426428
427429 @ Override
0 commit comments