Skip to content

Commit acbb9d1

Browse files
committed
minor code cleanup in Cascade.cascade
1 parent b96ec13 commit acbb9d1

File tree

2 files changed

+40
-45
lines changed

2 files changed

+40
-45
lines changed

hibernate-core/src/main/java/org/hibernate/engine/internal/Cascade.java

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.util.List;
1111

1212
import org.hibernate.HibernateException;
13-
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor;
1413
import org.hibernate.collection.spi.PersistentCollection;
1514
import org.hibernate.engine.spi.CascadeStyle;
1615
import 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(

hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4295,7 +4295,7 @@ protected void linkToSession(Object entity, SharedSessionContractImplementor ses
42954295

42964296
private void setSession(PersistentAttributeInterceptable entity, SharedSessionContractImplementor session) {
42974297
final BytecodeLazyAttributeInterceptor interceptor =
4298-
getEntityMetamodel().getBytecodeEnhancementMetadata()
4298+
entityMetamodel.getBytecodeEnhancementMetadata()
42994299
.extractLazyInterceptor( entity );
43004300
if ( interceptor != null ) {
43014301
interceptor.setSession( session );
@@ -4338,9 +4338,9 @@ && hasSubclasses()
43384338
// todo (6.0) : this previously used `org.hibernate.tuple.entity.EntityTuplizer#determineConcreteSubclassEntityName`
43394339
// - we may need something similar here...
43404340
for ( EntityMappingType subclassMappingType : subclassMappingTypes.values() ) {
4341-
if ( subclassMappingType.getEntityPersister().getRepresentationStrategy()
4342-
.getInstantiator().isSameClass(instance ) ) {
4343-
return subclassMappingType.getEntityPersister();
4341+
final EntityPersister persister = subclassMappingType.getEntityPersister();
4342+
if ( persister.getRepresentationStrategy().getInstantiator().isSameClass( instance ) ) {
4343+
return persister;
43444344
}
43454345
}
43464346
}

0 commit comments

Comments
 (0)