10
10
import java .util .List ;
11
11
12
12
import org .hibernate .HibernateException ;
13
- import org .hibernate .bytecode .enhance .spi .interceptor .LazyAttributeLoadingInterceptor ;
14
13
import org .hibernate .collection .spi .PersistentCollection ;
15
14
import org .hibernate .engine .spi .CascadeStyle ;
16
15
import org .hibernate .engine .spi .CascadingAction ;
@@ -89,12 +88,10 @@ public static <T> void cascade(
89
88
if ( traceEnabled ) {
90
89
LOG .tracev ( "Processing cascade {0} for: {1}" , action , persister .getEntityName () );
91
90
}
92
- final PersistenceContext persistenceContext = eventSource .getPersistenceContextInternal ();
93
- final boolean enhancedForLazyLoading =
94
- persister .getBytecodeEnhancementMetadata ().isEnhancedForLazyLoading ();
91
+ final var bytecodeEnhancement = persister .getBytecodeEnhancementMetadata ();
95
92
final EntityEntry entry ;
96
- if ( enhancedForLazyLoading ) {
97
- entry = persistenceContext .getEntry ( parent );
93
+ if ( bytecodeEnhancement . isEnhancedForLazyLoading () ) {
94
+ entry = eventSource . getPersistenceContextInternal () .getEntry ( parent );
98
95
if ( entry != null
99
96
&& entry .getLoadedState () == null
100
97
&& entry .getStatus () == Status .MANAGED ) {
@@ -104,24 +101,26 @@ public static <T> void cascade(
104
101
else {
105
102
entry = null ;
106
103
}
104
+
107
105
final Type [] types = persister .getPropertyTypes ();
108
106
final String [] propertyNames = persister .getPropertyNames ();
109
107
final CascadeStyle [] cascadeStyles = persister .getPropertyCascadeStyles ();
110
- final boolean hasUninitializedLazyProperties = persister . hasUninitializedLazyProperties ( parent );
108
+ final boolean hasUninitializedLazyProperties = bytecodeEnhancement . hasUnFetchedAttributes ( parent );
111
109
112
110
for ( int i = 0 ; i < types .length ; i ++) {
113
111
final CascadeStyle style = cascadeStyles [ i ];
114
112
final String propertyName = propertyNames [ i ];
115
113
final Type type = types [i ];
116
114
final boolean isUninitializedProperty =
117
115
hasUninitializedLazyProperties
118
- && !persister .getBytecodeEnhancementMetadata ()
119
- .isAttributeLoaded ( parent , propertyName );
116
+ && !bytecodeEnhancement .isAttributeLoaded ( parent , propertyName );
120
117
121
118
if ( action .appliesTo ( type , style ) ) {
122
119
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 ();
125
124
// parent is a bytecode enhanced entity.
126
125
// Cascade to an uninitialized, lazy value only if
127
126
// parent is managed in the PersistenceContext.
@@ -132,31 +131,26 @@ public static <T> void cascade(
132
131
// parent was not in the PersistenceContext
133
132
continue ;
134
133
}
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
137
136
// that corresponds to the uninitialized collection from the
138
137
// PersistenceContext. If not present, an uninitialized
139
138
// PersistentCollection will be added to the PersistenceContext.
140
139
// 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.
142
142
child = collectionType .getCollection (
143
143
collectionType .getKeyOfOwner ( parent , eventSource ),
144
144
eventSource ,
145
145
parent ,
146
146
null
147
147
);
148
148
}
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
- }
153
149
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 );
160
154
161
155
}
162
156
else {
@@ -181,21 +175,21 @@ else if ( action.performOnLazyProperty() && type instanceof EntityType ) {
181
175
false
182
176
);
183
177
}
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
+ );
199
193
}
200
194
}
201
195
@@ -387,10 +381,11 @@ private static boolean isForeignKeyToParent(Type type) {
387
381
*
388
382
* @param type The type representing the attribute metadata
389
383
*
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
391
385
*/
392
386
private static boolean isLogicalOneToOne (Type type ) {
393
- return type instanceof EntityType entityType && entityType .isLogicalOneToOne ();
387
+ return type instanceof EntityType entityType
388
+ && entityType .isLogicalOneToOne ();
394
389
}
395
390
396
391
private static boolean cascadeAssociationNow (
0 commit comments