3636import org .hibernate .type .OneToOneType ;
3737import org .hibernate .type .Type ;
3838
39+ import static java .util .Collections .EMPTY_LIST ;
3940import static org .hibernate .engine .internal .ManagedTypeHelper .isHibernateProxy ;
4041import static org .hibernate .engine .spi .CascadingActions .CHECK_ON_FLUSH ;
4142import static org .hibernate .pretty .MessageHelper .infoString ;
@@ -89,7 +90,8 @@ public static <T> void cascade(
8990 LOG .tracev ( "Processing cascade {0} for: {1}" , action , persister .getEntityName () );
9091 }
9192 final PersistenceContext persistenceContext = eventSource .getPersistenceContextInternal ();
92- final boolean enhancedForLazyLoading = persister .getBytecodeEnhancementMetadata ().isEnhancedForLazyLoading ();
93+ final boolean enhancedForLazyLoading =
94+ persister .getBytecodeEnhancementMetadata ().isEnhancedForLazyLoading ();
9395 final EntityEntry entry ;
9496 if ( enhancedForLazyLoading ) {
9597 entry = persistenceContext .getEntry ( parent );
@@ -112,8 +114,9 @@ public static <T> void cascade(
112114 final String propertyName = propertyNames [ i ];
113115 final Type type = types [i ];
114116 final boolean isUninitializedProperty =
115- hasUninitializedLazyProperties &&
116- !persister .getBytecodeEnhancementMetadata ().isAttributeLoaded ( parent , propertyName );
117+ hasUninitializedLazyProperties
118+ && !persister .getBytecodeEnhancementMetadata ()
119+ .isAttributeLoaded ( parent , propertyName );
117120
118121 if ( style .doCascade ( action ) ) {
119122 final Object child ;
@@ -129,14 +132,13 @@ public static <T> void cascade(
129132 // parent was not in the PersistenceContext
130133 continue ;
131134 }
132- if ( type instanceof CollectionType ) {
135+ if ( type instanceof CollectionType collectionType ) {
133136 // CollectionType#getCollection gets the PersistentCollection
134137 // that corresponds to the uninitialized collection from the
135138 // PersistenceContext. If not present, an uninitialized
136139 // PersistentCollection will be added to the PersistenceContext.
137140 // The action may initialize it later, if necessary.
138141 // This needs to be done even when action.performOnLazyProperty() returns false.
139- final CollectionType collectionType = (CollectionType ) type ;
140142 child = collectionType .getCollection (
141143 collectionType .getKeyOfOwner ( parent , eventSource ),
142144 eventSource ,
@@ -146,15 +148,14 @@ public static <T> void cascade(
146148 }
147149 else if ( type instanceof AnyType || type instanceof ComponentType ) {
148150 // Hibernate does not support lazy embeddables, so this shouldn't happen.
149- throw new UnsupportedOperationException (
150- "Lazy components are not supported."
151- );
151+ throw new UnsupportedOperationException ( "Lazy embeddables are not supported" );
152152 }
153153 else if ( action .performOnLazyProperty () && type instanceof EntityType ) {
154154 // Only need to initialize a lazy entity attribute when action.performOnLazyProperty()
155155 // returns true.
156- LazyAttributeLoadingInterceptor interceptor = persister .getBytecodeEnhancementMetadata ()
157- .extractInterceptor ( parent );
156+ final LazyAttributeLoadingInterceptor interceptor =
157+ persister .getBytecodeEnhancementMetadata ()
158+ .extractInterceptor ( parent );
158159 child = interceptor .fetchAttribute ( parent , propertyName );
159160
160161 }
@@ -238,10 +239,10 @@ private static <T> void cascadeProperty(
238239 style ,
239240 anything ,
240241 isCascadeDeleteEnabled
241- );
242+ );
242243 }
243244 }
244- else if ( type instanceof ComponentType ) {
245+ else if ( type instanceof ComponentType componentType ) {
245246 if ( componentPath == null && propertyName != null ) {
246247 componentPath = new ArrayList <>();
247248 }
@@ -255,7 +256,7 @@ else if ( type instanceof ComponentType ) {
255256 componentPath ,
256257 parent ,
257258 child ,
258- ( CompositeType ) type ,
259+ componentType ,
259260 anything
260261 );
261262 if ( componentPath != null ) {
@@ -273,7 +274,8 @@ else if ( type instanceof ComponentType ) {
273274 type ,
274275 style ,
275276 propertyName ,
276- isCascadeDeleteEnabled );
277+ isCascadeDeleteEnabled
278+ );
277279 }
278280 }
279281
@@ -376,8 +378,8 @@ private static <T> void cascadeLogicalOneToOneOrphanRemoval(
376378
377379 private static boolean isForeignKeyToParent (Type type ) {
378380 return type instanceof CollectionType
379- || type instanceof OneToOneType
380- && (( OneToOneType ) type ) .getForeignKeyDirection () == ForeignKeyDirection .TO_PARENT ;
381+ || type instanceof OneToOneType oneToOneType
382+ && oneToOneType .getForeignKeyDirection () == ForeignKeyDirection .TO_PARENT ;
381383 }
382384
383385 /**
@@ -389,7 +391,7 @@ private static boolean isForeignKeyToParent(Type type) {
389391 * @return True if the attribute represents a logical one to one association
390392 */
391393 private static boolean isLogicalOneToOne (Type type ) {
392- return type instanceof EntityType && ( ( EntityType ) type ) .isLogicalOneToOne ();
394+ return type instanceof EntityType entityType && entityType .isLogicalOneToOne ();
393395 }
394396
395397 private static boolean cascadeAssociationNow (
@@ -399,24 +401,22 @@ private static boolean cascadeAssociationNow(
399401 SessionFactoryImplementor factory ,
400402 boolean unownedTransient ) {
401403 return associationType .getForeignKeyDirection ().cascadeNow ( cascadePoint )
402- // For check on flush, we should only check unowned associations when strictness is enforced
403- && ( action != CHECK_ON_FLUSH || unownedTransient || !isUnownedAssociation ( associationType , factory ) );
404+ // For check on flush, we should only check unowned associations when strictness is enforced
405+ && ( action != CHECK_ON_FLUSH || unownedTransient || !isUnownedAssociation ( associationType , factory ) );
404406 }
405407
406408 private static boolean isUnownedAssociation (AssociationType associationType , SessionFactoryImplementor factory ) {
407- if ( associationType instanceof ManyToOneType ) {
408- final ManyToOneType manyToOne = (ManyToOneType ) associationType ;
409+ if ( associationType instanceof ManyToOneType manyToOne ) {
409410 // logical one-to-one + non-null unique key property name indicates unowned
410411 return manyToOne .isLogicalOneToOne () && manyToOne .getRHSUniqueKeyPropertyName () != null ;
411412 }
412- else if ( associationType instanceof OneToOneType ) {
413- final OneToOneType oneToOne = (OneToOneType ) associationType ;
413+ else if ( associationType instanceof OneToOneType oneToOne ) {
414414 // constrained false + non-null unique key property name indicates unowned
415415 return oneToOne .isNullable () && oneToOne .getRHSUniqueKeyPropertyName () != null ;
416416 }
417- else if ( associationType instanceof CollectionType ) {
417+ else if ( associationType instanceof CollectionType collectionType ) {
418418 // for collections, we can ask the persister if we're on the inverse side
419- return ( ( CollectionType ) associationType ) .isInverse ( factory );
419+ return collectionType .isInverse ( factory );
420420 }
421421 return false ;
422422 }
@@ -454,7 +454,7 @@ private static <T> void cascadeComponent(
454454 subPropertyName ,
455455 anything ,
456456 false
457- );
457+ );
458458 }
459459 }
460460 }
@@ -473,7 +473,7 @@ private static <T> void cascadeAssociation(
473473 if ( type instanceof EntityType || type instanceof AnyType ) {
474474 cascadeToOne ( action , eventSource , parent , child , type , style , anything , isCascadeDeleteEnabled );
475475 }
476- else if ( type instanceof CollectionType ) {
476+ else if ( type instanceof CollectionType collectionType ) {
477477 cascadeCollection (
478478 action ,
479479 cascadePoint ,
@@ -483,7 +483,7 @@ else if ( type instanceof CollectionType ) {
483483 child ,
484484 style ,
485485 anything ,
486- ( CollectionType ) type
486+ collectionType
487487 );
488488 }
489489 }
@@ -505,17 +505,13 @@ private static <T> void cascadeCollection(
505505 eventSource .getFactory ().getMappingMetamodel ()
506506 .getCollectionDescriptor ( type .getRole () );
507507 final Type elemType = persister .getElementType ();
508-
509- CascadePoint elementsCascadePoint = cascadePoint ;
510- if ( cascadePoint == CascadePoint .AFTER_INSERT_BEFORE_DELETE ) {
511- elementsCascadePoint = CascadePoint .AFTER_INSERT_BEFORE_DELETE_VIA_COLLECTION ;
512- }
513-
514508 //cascade to current collection elements
515509 if ( elemType instanceof EntityType || elemType instanceof AnyType || elemType instanceof ComponentType ) {
516510 cascadeCollectionElements (
517511 action ,
518- elementsCascadePoint ,
512+ cascadePoint == CascadePoint .AFTER_INSERT_BEFORE_DELETE
513+ ? CascadePoint .AFTER_INSERT_BEFORE_DELETE_VIA_COLLECTION
514+ : cascadePoint ,
519515 eventSource ,
520516 componentPath ,
521517 parent ,
@@ -541,9 +537,10 @@ private static <T> void cascadeToOne(
541537 final CascadeStyle style ,
542538 final T anything ,
543539 final boolean isCascadeDeleteEnabled ) {
544- final String entityName = type instanceof EntityType
545- ? ( (EntityType ) type ).getAssociatedEntityName ()
546- : null ;
540+ final String entityName =
541+ type instanceof EntityType entityType
542+ ? entityType .getAssociatedEntityName ()
543+ : null ;
547544 if ( style .reallyDoCascade ( action ) ) {
548545 //not really necessary, but good for consistency...
549546 final PersistenceContext persistenceContext = eventSource .getPersistenceContextInternal ();
@@ -572,6 +569,7 @@ private static <T> void cascadeCollectionElements(
572569 final Type elemType ,
573570 final T anything ,
574571 final boolean isCascadeDeleteEnabled ) throws HibernateException {
572+
575573 final boolean reallyDoCascade = style .reallyDoCascade ( action )
576574 && child != CollectionType .UNFETCHED_COLLECTION ;
577575
@@ -581,15 +579,15 @@ private static <T> void cascadeCollectionElements(
581579 LOG .tracev ( "Cascade {0} for collection: {1}" , action , collectionType .getRole () );
582580 }
583581
584- final Iterator <?> itr = action .getCascadableChildrenIterator ( eventSource , collectionType , child );
585- while ( itr .hasNext () ) {
582+ final Iterator <?> iterator = action .getCascadableChildrenIterator ( eventSource , collectionType , child );
583+ while ( iterator .hasNext () ) {
586584 cascadeProperty (
587585 action ,
588586 cascadePoint ,
589587 eventSource ,
590588 componentPath ,
591589 parent ,
592- itr .next (),
590+ iterator .next (),
593591 elemType ,
594592 style ,
595593 collectionType .getRole ().substring ( collectionType .getRole ().lastIndexOf ('.' ) + 1 ),
@@ -606,9 +604,9 @@ private static <T> void cascadeCollectionElements(
606604 final boolean deleteOrphans = style .hasOrphanDelete ()
607605 && action .deleteOrphans ()
608606 && elemType instanceof EntityType
609- && child instanceof PersistentCollection
607+ && child instanceof PersistentCollection <?> persistentCollection
610608 // a newly instantiated collection can't have orphans
611- && ! ( ( PersistentCollection <?>) child ) .isNewlyInstantiated ();
609+ && !persistentCollection .isNewlyInstantiated ();
612610
613611 if ( deleteOrphans ) {
614612 final boolean traceEnabled = LOG .isTraceEnabled ();
@@ -634,10 +632,8 @@ private static void deleteOrphans(EventSource eventSource, String entityName, Pe
634632 //TODO: suck this logic into the collection!
635633 final Collection <?> orphans ;
636634 if ( pc .wasInitialized () ) {
637- final CollectionEntry ce = eventSource .getPersistenceContextInternal ().getCollectionEntry ( pc );
638- orphans = ce ==null
639- ? java .util .Collections .EMPTY_LIST
640- : ce .getOrphans ( entityName , pc );
635+ final CollectionEntry entry = eventSource .getPersistenceContextInternal ().getCollectionEntry ( pc );
636+ orphans = entry == null ? EMPTY_LIST : entry .getOrphans ( entityName , pc );
641637 }
642638 else {
643639 orphans = pc .getQueuedOrphans ( entityName );
0 commit comments