36
36
import org .hibernate .type .OneToOneType ;
37
37
import org .hibernate .type .Type ;
38
38
39
+ import static java .util .Collections .EMPTY_LIST ;
39
40
import static org .hibernate .engine .internal .ManagedTypeHelper .isHibernateProxy ;
40
41
import static org .hibernate .engine .spi .CascadingActions .CHECK_ON_FLUSH ;
41
42
import static org .hibernate .pretty .MessageHelper .infoString ;
@@ -89,7 +90,8 @@ public static <T> void cascade(
89
90
LOG .tracev ( "Processing cascade {0} for: {1}" , action , persister .getEntityName () );
90
91
}
91
92
final PersistenceContext persistenceContext = eventSource .getPersistenceContextInternal ();
92
- final boolean enhancedForLazyLoading = persister .getBytecodeEnhancementMetadata ().isEnhancedForLazyLoading ();
93
+ final boolean enhancedForLazyLoading =
94
+ persister .getBytecodeEnhancementMetadata ().isEnhancedForLazyLoading ();
93
95
final EntityEntry entry ;
94
96
if ( enhancedForLazyLoading ) {
95
97
entry = persistenceContext .getEntry ( parent );
@@ -112,8 +114,9 @@ public static <T> void cascade(
112
114
final String propertyName = propertyNames [ i ];
113
115
final Type type = types [i ];
114
116
final boolean isUninitializedProperty =
115
- hasUninitializedLazyProperties &&
116
- !persister .getBytecodeEnhancementMetadata ().isAttributeLoaded ( parent , propertyName );
117
+ hasUninitializedLazyProperties
118
+ && !persister .getBytecodeEnhancementMetadata ()
119
+ .isAttributeLoaded ( parent , propertyName );
117
120
118
121
if ( style .doCascade ( action ) ) {
119
122
final Object child ;
@@ -129,14 +132,13 @@ public static <T> void cascade(
129
132
// parent was not in the PersistenceContext
130
133
continue ;
131
134
}
132
- if ( type instanceof CollectionType ) {
135
+ if ( type instanceof CollectionType collectionType ) {
133
136
// CollectionType#getCollection gets the PersistentCollection
134
137
// that corresponds to the uninitialized collection from the
135
138
// PersistenceContext. If not present, an uninitialized
136
139
// PersistentCollection will be added to the PersistenceContext.
137
140
// The action may initialize it later, if necessary.
138
141
// This needs to be done even when action.performOnLazyProperty() returns false.
139
- final CollectionType collectionType = (CollectionType ) type ;
140
142
child = collectionType .getCollection (
141
143
collectionType .getKeyOfOwner ( parent , eventSource ),
142
144
eventSource ,
@@ -146,15 +148,14 @@ public static <T> void cascade(
146
148
}
147
149
else if ( type instanceof AnyType || type instanceof ComponentType ) {
148
150
// 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" );
152
152
}
153
153
else if ( action .performOnLazyProperty () && type instanceof EntityType ) {
154
154
// Only need to initialize a lazy entity attribute when action.performOnLazyProperty()
155
155
// returns true.
156
- LazyAttributeLoadingInterceptor interceptor = persister .getBytecodeEnhancementMetadata ()
157
- .extractInterceptor ( parent );
156
+ final LazyAttributeLoadingInterceptor interceptor =
157
+ persister .getBytecodeEnhancementMetadata ()
158
+ .extractInterceptor ( parent );
158
159
child = interceptor .fetchAttribute ( parent , propertyName );
159
160
160
161
}
@@ -238,10 +239,10 @@ private static <T> void cascadeProperty(
238
239
style ,
239
240
anything ,
240
241
isCascadeDeleteEnabled
241
- );
242
+ );
242
243
}
243
244
}
244
- else if ( type instanceof ComponentType ) {
245
+ else if ( type instanceof ComponentType componentType ) {
245
246
if ( componentPath == null && propertyName != null ) {
246
247
componentPath = new ArrayList <>();
247
248
}
@@ -255,7 +256,7 @@ else if ( type instanceof ComponentType ) {
255
256
componentPath ,
256
257
parent ,
257
258
child ,
258
- ( CompositeType ) type ,
259
+ componentType ,
259
260
anything
260
261
);
261
262
if ( componentPath != null ) {
@@ -273,7 +274,8 @@ else if ( type instanceof ComponentType ) {
273
274
type ,
274
275
style ,
275
276
propertyName ,
276
- isCascadeDeleteEnabled );
277
+ isCascadeDeleteEnabled
278
+ );
277
279
}
278
280
}
279
281
@@ -376,8 +378,8 @@ private static <T> void cascadeLogicalOneToOneOrphanRemoval(
376
378
377
379
private static boolean isForeignKeyToParent (Type type ) {
378
380
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 ;
381
383
}
382
384
383
385
/**
@@ -389,7 +391,7 @@ private static boolean isForeignKeyToParent(Type type) {
389
391
* @return True if the attribute represents a logical one to one association
390
392
*/
391
393
private static boolean isLogicalOneToOne (Type type ) {
392
- return type instanceof EntityType && ( ( EntityType ) type ) .isLogicalOneToOne ();
394
+ return type instanceof EntityType entityType && entityType .isLogicalOneToOne ();
393
395
}
394
396
395
397
private static boolean cascadeAssociationNow (
@@ -399,24 +401,22 @@ private static boolean cascadeAssociationNow(
399
401
SessionFactoryImplementor factory ,
400
402
boolean unownedTransient ) {
401
403
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 ) );
404
406
}
405
407
406
408
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 ) {
409
410
// logical one-to-one + non-null unique key property name indicates unowned
410
411
return manyToOne .isLogicalOneToOne () && manyToOne .getRHSUniqueKeyPropertyName () != null ;
411
412
}
412
- else if ( associationType instanceof OneToOneType ) {
413
- final OneToOneType oneToOne = (OneToOneType ) associationType ;
413
+ else if ( associationType instanceof OneToOneType oneToOne ) {
414
414
// constrained false + non-null unique key property name indicates unowned
415
415
return oneToOne .isNullable () && oneToOne .getRHSUniqueKeyPropertyName () != null ;
416
416
}
417
- else if ( associationType instanceof CollectionType ) {
417
+ else if ( associationType instanceof CollectionType collectionType ) {
418
418
// 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 );
420
420
}
421
421
return false ;
422
422
}
@@ -454,7 +454,7 @@ private static <T> void cascadeComponent(
454
454
subPropertyName ,
455
455
anything ,
456
456
false
457
- );
457
+ );
458
458
}
459
459
}
460
460
}
@@ -473,7 +473,7 @@ private static <T> void cascadeAssociation(
473
473
if ( type instanceof EntityType || type instanceof AnyType ) {
474
474
cascadeToOne ( action , eventSource , parent , child , type , style , anything , isCascadeDeleteEnabled );
475
475
}
476
- else if ( type instanceof CollectionType ) {
476
+ else if ( type instanceof CollectionType collectionType ) {
477
477
cascadeCollection (
478
478
action ,
479
479
cascadePoint ,
@@ -483,7 +483,7 @@ else if ( type instanceof CollectionType ) {
483
483
child ,
484
484
style ,
485
485
anything ,
486
- ( CollectionType ) type
486
+ collectionType
487
487
);
488
488
}
489
489
}
@@ -505,17 +505,13 @@ private static <T> void cascadeCollection(
505
505
eventSource .getFactory ().getMappingMetamodel ()
506
506
.getCollectionDescriptor ( type .getRole () );
507
507
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
-
514
508
//cascade to current collection elements
515
509
if ( elemType instanceof EntityType || elemType instanceof AnyType || elemType instanceof ComponentType ) {
516
510
cascadeCollectionElements (
517
511
action ,
518
- elementsCascadePoint ,
512
+ cascadePoint == CascadePoint .AFTER_INSERT_BEFORE_DELETE
513
+ ? CascadePoint .AFTER_INSERT_BEFORE_DELETE_VIA_COLLECTION
514
+ : cascadePoint ,
519
515
eventSource ,
520
516
componentPath ,
521
517
parent ,
@@ -541,9 +537,10 @@ private static <T> void cascadeToOne(
541
537
final CascadeStyle style ,
542
538
final T anything ,
543
539
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 ;
547
544
if ( style .reallyDoCascade ( action ) ) {
548
545
//not really necessary, but good for consistency...
549
546
final PersistenceContext persistenceContext = eventSource .getPersistenceContextInternal ();
@@ -572,6 +569,7 @@ private static <T> void cascadeCollectionElements(
572
569
final Type elemType ,
573
570
final T anything ,
574
571
final boolean isCascadeDeleteEnabled ) throws HibernateException {
572
+
575
573
final boolean reallyDoCascade = style .reallyDoCascade ( action )
576
574
&& child != CollectionType .UNFETCHED_COLLECTION ;
577
575
@@ -581,15 +579,15 @@ private static <T> void cascadeCollectionElements(
581
579
LOG .tracev ( "Cascade {0} for collection: {1}" , action , collectionType .getRole () );
582
580
}
583
581
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 () ) {
586
584
cascadeProperty (
587
585
action ,
588
586
cascadePoint ,
589
587
eventSource ,
590
588
componentPath ,
591
589
parent ,
592
- itr .next (),
590
+ iterator .next (),
593
591
elemType ,
594
592
style ,
595
593
collectionType .getRole ().substring ( collectionType .getRole ().lastIndexOf ('.' ) + 1 ),
@@ -606,9 +604,9 @@ private static <T> void cascadeCollectionElements(
606
604
final boolean deleteOrphans = style .hasOrphanDelete ()
607
605
&& action .deleteOrphans ()
608
606
&& elemType instanceof EntityType
609
- && child instanceof PersistentCollection
607
+ && child instanceof PersistentCollection <?> persistentCollection
610
608
// a newly instantiated collection can't have orphans
611
- && ! ( ( PersistentCollection <?>) child ) .isNewlyInstantiated ();
609
+ && !persistentCollection .isNewlyInstantiated ();
612
610
613
611
if ( deleteOrphans ) {
614
612
final boolean traceEnabled = LOG .isTraceEnabled ();
@@ -634,10 +632,8 @@ private static void deleteOrphans(EventSource eventSource, String entityName, Pe
634
632
//TODO: suck this logic into the collection!
635
633
final Collection <?> orphans ;
636
634
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 );
641
637
}
642
638
else {
643
639
orphans = pc .getQueuedOrphans ( entityName );
0 commit comments