1010import java .util .List ;
1111
1212import org .hibernate .HibernateException ;
13- import org .hibernate .bytecode .enhance .spi .interceptor .LazyAttributeLoadingInterceptor ;
1413import org .hibernate .collection .spi .PersistentCollection ;
1514import org .hibernate .engine .spi .CascadeStyle ;
1615import org .hibernate .engine .spi .CascadingAction ;
@@ -89,12 +88,10 @@ public static <T> void cascade(
8988 if ( traceEnabled ) {
9089 LOG .tracev ( "Processing cascade {0} for: {1}" , action , persister .getEntityName () );
9190 }
92- final PersistenceContext persistenceContext = eventSource .getPersistenceContextInternal ();
93- final boolean enhancedForLazyLoading =
94- persister .getBytecodeEnhancementMetadata ().isEnhancedForLazyLoading ();
91+ final var bytecodeEnhancement = persister .getBytecodeEnhancementMetadata ();
9592 final EntityEntry entry ;
96- if ( enhancedForLazyLoading ) {
97- entry = persistenceContext .getEntry ( parent );
93+ if ( bytecodeEnhancement . isEnhancedForLazyLoading () ) {
94+ entry = eventSource . getPersistenceContextInternal () .getEntry ( parent );
9895 if ( entry != null
9996 && entry .getLoadedState () == null
10097 && entry .getStatus () == Status .MANAGED ) {
@@ -104,24 +101,26 @@ public static <T> void cascade(
104101 else {
105102 entry = null ;
106103 }
104+
107105 final Type [] types = persister .getPropertyTypes ();
108106 final String [] propertyNames = persister .getPropertyNames ();
109107 final CascadeStyle [] cascadeStyles = persister .getPropertyCascadeStyles ();
110- final boolean hasUninitializedLazyProperties = persister . hasUninitializedLazyProperties ( parent );
108+ final boolean hasUninitializedLazyProperties = bytecodeEnhancement . hasUnFetchedAttributes ( parent );
111109
112110 for ( int i = 0 ; i < types .length ; i ++) {
113111 final CascadeStyle style = cascadeStyles [ i ];
114112 final String propertyName = propertyNames [ i ];
115113 final Type type = types [i ];
116114 final boolean isUninitializedProperty =
117115 hasUninitializedLazyProperties
118- && !persister .getBytecodeEnhancementMetadata ()
119- .isAttributeLoaded ( parent , propertyName );
116+ && !bytecodeEnhancement .isAttributeLoaded ( parent , propertyName );
120117
121118 if ( action .appliesTo ( type , style ) ) {
122119 final Object child ;
123- if ( isUninitializedProperty ) {
124- assert enhancedForLazyLoading ;
120+ if ( isUninitializedProperty ) {
121+ assert bytecodeEnhancement .isEnhancedForLazyLoading ();
122+ // Hibernate does not support lazy embeddables
123+ assert !type .isComponentType ();
125124 // parent is a bytecode enhanced entity.
126125 // Cascade to an uninitialized, lazy value only if
127126 // parent is managed in the PersistenceContext.
@@ -132,31 +131,26 @@ public static <T> void cascade(
132131 // parent was not in the PersistenceContext
133132 continue ;
134133 }
135- if ( type instanceof CollectionType collectionType ) {
136- // CollectionType# getCollection gets the PersistentCollection
134+ else if ( type instanceof CollectionType collectionType ) {
135+ // CollectionType. getCollection() gets the PersistentCollection
137136 // that corresponds to the uninitialized collection from the
138137 // PersistenceContext. If not present, an uninitialized
139138 // PersistentCollection will be added to the PersistenceContext.
140139 // The action may initialize it later, if necessary.
141- // This needs to be done even when action.performOnLazyProperty() returns false.
140+ // This needs to be done even when action.performOnLazyProperty()
141+ // returns false.
142142 child = collectionType .getCollection (
143143 collectionType .getKeyOfOwner ( parent , eventSource ),
144144 eventSource ,
145145 parent ,
146146 null
147147 );
148148 }
149- else if ( type instanceof AnyType || type instanceof ComponentType ) {
150- // Hibernate does not support lazy embeddables, so this shouldn't happen.
151- throw new UnsupportedOperationException ( "Lazy embeddables are not supported" );
152- }
153149 else if ( action .performOnLazyProperty () && type instanceof EntityType ) {
154- // Only need to initialize a lazy entity attribute when action.performOnLazyProperty()
155- // returns true.
156- final LazyAttributeLoadingInterceptor interceptor =
157- persister .getBytecodeEnhancementMetadata ()
158- .extractInterceptor ( parent );
159- child = interceptor .fetchAttribute ( parent , propertyName );
150+ // Only need to initialize a lazy entity attribute when
151+ // action.performOnLazyProperty() returns true.
152+ child = bytecodeEnhancement .extractInterceptor ( parent )
153+ .fetchAttribute ( parent , propertyName );
160154
161155 }
162156 else {
@@ -181,21 +175,21 @@ else if ( action.performOnLazyProperty() && type instanceof EntityType ) {
181175 false
182176 );
183177 }
184- else {
185- // If the property is uninitialized, then there cannot be any orphans.
186- if ( action . deleteOrphans () && !isUninitializedProperty && isLogicalOneToOne ( type ) ) {
187- cascadeLogicalOneToOneOrphanRemoval (
188- action ,
189- eventSource ,
190- null ,
191- parent ,
192- persister . getValue ( parent , i ) ,
193- type ,
194- style ,
195- propertyName ,
196- false
197- );
198- }
178+ else if ( action . deleteOrphans ()
179+ // If the property is uninitialized, there cannot be any orphans.
180+ && !isUninitializedProperty
181+ && isLogicalOneToOne ( type ) ) {
182+ cascadeLogicalOneToOneOrphanRemoval (
183+ action ,
184+ eventSource ,
185+ null ,
186+ parent ,
187+ persister . getValue ( parent , i ) ,
188+ type ,
189+ style ,
190+ propertyName ,
191+ false
192+ );
199193 }
200194 }
201195
@@ -387,10 +381,11 @@ private static boolean isForeignKeyToParent(Type type) {
387381 *
388382 * @param type The type representing the attribute metadata
389383 *
390- * @return True if the attribute represents a logical one to one association
384+ * @return True if the attribute represents a logical one-to- one association
391385 */
392386 private static boolean isLogicalOneToOne (Type type ) {
393- return type instanceof EntityType entityType && entityType .isLogicalOneToOne ();
387+ return type instanceof EntityType entityType
388+ && entityType .isLogicalOneToOne ();
394389 }
395390
396391 private static boolean cascadeAssociationNow (
0 commit comments