Skip to content

Commit 4bb02cd

Browse files
committed
HHH-18506 Improve flush performance by reducing itable stubs
1 parent f261d87 commit 4bb02cd

35 files changed

+219
-148
lines changed

hibernate-core/src/main/java/org/hibernate/boot/beanvalidation/HibernateTraversableResolver.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
import org.hibernate.Hibernate;
1616
import org.hibernate.engine.spi.SessionFactoryImplementor;
1717
import org.hibernate.persister.entity.EntityPersister;
18+
import org.hibernate.type.AnyType;
1819
import org.hibernate.type.CollectionType;
20+
import org.hibernate.type.ComponentType;
1921
import org.hibernate.type.CompositeType;
22+
import org.hibernate.type.EntityType;
2023
import org.hibernate.type.Type;
2124

2225
import jakarta.validation.Path;
@@ -54,17 +57,17 @@ private void addAssociationsToTheSetForAllProperties(String[] names, Type[] type
5457

5558
private void addAssociationsToTheSetForOneProperty(String name, Type type, String prefix, SessionFactoryImplementor factory) {
5659

57-
if ( type.isCollectionType() ) {
60+
if ( type instanceof CollectionType ) {
5861
CollectionType collType = (CollectionType) type;
5962
Type assocType = collType.getElementType( factory );
6063
addAssociationsToTheSetForOneProperty(name, assocType, prefix, factory);
6164
}
6265
//ToOne association
63-
else if ( type.isEntityType() || type.isAnyType() ) {
66+
else if ( type instanceof EntityType || type instanceof AnyType ) {
6467
associations.add( prefix + name );
6568
}
66-
else if ( type.isComponentType() ) {
67-
CompositeType componentType = (CompositeType) type;
69+
else if ( type instanceof ComponentType ) {
70+
ComponentType componentType = (ComponentType) type;
6871
addAssociationsToTheSetForAllProperties(
6972
componentType.getPropertyNames(),
7073
componentType.getSubtypes(),

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/EnhancementAsProxyLazinessInterceptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.hibernate.internal.util.collections.ArrayHelper;
2020
import org.hibernate.metamodel.mapping.AttributeMapping;
2121
import org.hibernate.persister.entity.EntityPersister;
22+
import org.hibernate.type.CollectionType;
2223
import org.hibernate.type.CompositeType;
2324
import org.hibernate.type.Type;
2425

@@ -67,7 +68,7 @@ public EnhancementAsProxyLazinessInterceptor(
6768
collectionAttributeNames = new HashSet<>();
6869
for ( int i = 0; i < propertyTypes.length; i++ ) {
6970
Type propertyType = propertyTypes[i];
70-
if ( propertyType.isCollectionType() ) {
71+
if ( propertyType instanceof CollectionType ) {
7172
collectionAttributeNames.add( entityPersister.getPropertyNames()[i] );
7273
}
7374
}

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/LazyAttributeDescriptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.hibernate.bytecode.enhance.spi.interceptor;
88

99
import org.hibernate.mapping.Property;
10+
import org.hibernate.type.CollectionType;
1011
import org.hibernate.type.Type;
1112

1213
/**
@@ -21,7 +22,7 @@ public static LazyAttributeDescriptor from(
2122
int lazyIndex) {
2223
String fetchGroupName = property.getLazyGroup();
2324
if ( fetchGroupName == null ) {
24-
fetchGroupName = property.getType().isCollectionType()
25+
fetchGroupName = property.getType() instanceof CollectionType
2526
? property.getName()
2627
: "DEFAULT";
2728
}

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
import org.hibernate.persister.entity.EntityPersister;
2929
import org.hibernate.pretty.MessageHelper;
3030
import org.hibernate.proxy.HibernateProxy;
31+
import org.hibernate.type.AnyType;
3132
import org.hibernate.type.AssociationType;
3233
import org.hibernate.type.CollectionType;
3334
import org.hibernate.type.ComponentType;
3435
import org.hibernate.type.CompositeType;
3536
import org.hibernate.type.EntityType;
3637
import org.hibernate.type.ForeignKeyDirection;
38+
import org.hibernate.type.OneToOneType;
3739
import org.hibernate.type.Type;
3840

3941
import static org.hibernate.engine.internal.ManagedTypeHelper.isHibernateProxy;
@@ -125,7 +127,7 @@ public static <T> void cascade(
125127
// parent was not in the PersistenceContext
126128
continue;
127129
}
128-
if ( type.isCollectionType() ) {
130+
if ( type instanceof CollectionType ) {
129131
// CollectionType#getCollection gets the PersistentCollection
130132
// that corresponds to the uninitialized collection from the
131133
// PersistenceContext. If not present, an uninitialized
@@ -140,13 +142,13 @@ public static <T> void cascade(
140142
null
141143
);
142144
}
143-
else if ( type.isComponentType() ) {
145+
else if ( type instanceof AnyType || type instanceof ComponentType ) {
144146
// Hibernate does not support lazy embeddables, so this shouldn't happen.
145147
throw new UnsupportedOperationException(
146148
"Lazy components are not supported."
147149
);
148150
}
149-
else if ( action.performOnLazyProperty() && type.isEntityType() ) {
151+
else if ( action.performOnLazyProperty() && type instanceof EntityType ) {
150152
// Only need to initialize a lazy entity attribute when action.performOnLazyProperty()
151153
// returns true.
152154
LazyAttributeLoadingInterceptor interceptor = persister.getBytecodeEnhancementMetadata()
@@ -226,7 +228,7 @@ private static <T> void cascadeProperty(
226228
final boolean isCascadeDeleteEnabled) throws HibernateException {
227229

228230
if ( child != null ) {
229-
if ( type.isAssociationType() ) {
231+
if ( type instanceof EntityType || type instanceof CollectionType || type instanceof AnyType ) {
230232
final AssociationType associationType = (AssociationType) type;
231233
if ( cascadeAssociationNow( cascadePoint, associationType ) ) {
232234
cascadeAssociation(
@@ -243,7 +245,7 @@ private static <T> void cascadeProperty(
243245
);
244246
}
245247
}
246-
else if ( type.isComponentType() ) {
248+
else if ( type instanceof ComponentType ) {
247249
if ( componentPath == null && propertyName != null ) {
248250
componentPath = new ArrayList<>();
249251
}
@@ -358,9 +360,8 @@ private static <T> void cascadeLogicalOneToOneOrphanRemoval(
358360
LOG.tracev( "Deleting orphaned entity instance: {0}", description );
359361
}
360362

361-
if ( type.isAssociationType() && ( (AssociationType) type ).getForeignKeyDirection().equals(
362-
ForeignKeyDirection.TO_PARENT
363-
) ) {
363+
if ( type instanceof CollectionType
364+
|| type instanceof OneToOneType && ( (OneToOneType) type ).getForeignKeyDirection() == ForeignKeyDirection.TO_PARENT ) {
364365
// If FK direction is to-parent, we must remove the orphan *before* the queued update(s)
365366
// occur. Otherwise, replacing the association on a managed entity, without manually
366367
// nulling and flushing, causes FK constraint violations.
@@ -442,10 +443,10 @@ private static <T> void cascadeAssociation(
442443
final CascadeStyle style,
443444
final T anything,
444445
final boolean isCascadeDeleteEnabled) {
445-
if ( type.isEntityType() || type.isAnyType() ) {
446+
if ( type instanceof EntityType || type instanceof AnyType ) {
446447
cascadeToOne( action, eventSource, parent, child, type, style, anything, isCascadeDeleteEnabled );
447448
}
448-
else if ( type.isCollectionType() ) {
449+
else if ( type instanceof CollectionType ) {
449450
cascadeCollection(
450451
action,
451452
cascadePoint,
@@ -485,7 +486,7 @@ private static <T> void cascadeCollection(
485486
}
486487

487488
//cascade to current collection elements
488-
if ( elemType.isEntityType() || elemType.isAnyType() || elemType.isComponentType() ) {
489+
if ( elemType instanceof EntityType || elemType instanceof AnyType || elemType instanceof ComponentType ) {
489490
cascadeCollectionElements(
490491
action,
491492
elementsCascadePoint,
@@ -514,7 +515,7 @@ private static <T> void cascadeToOne(
514515
final CascadeStyle style,
515516
final T anything,
516517
final boolean isCascadeDeleteEnabled) {
517-
final String entityName = type.isEntityType()
518+
final String entityName = type instanceof EntityType
518519
? ( (EntityType) type ).getAssociatedEntityName()
519520
: null;
520521
if ( style.reallyDoCascade( action ) ) {
@@ -578,7 +579,7 @@ private static <T> void cascadeCollectionElements(
578579

579580
final boolean deleteOrphans = style.hasOrphanDelete()
580581
&& action.deleteOrphans()
581-
&& elemType.isEntityType()
582+
&& elemType instanceof EntityType
582583
&& child instanceof PersistentCollection
583584
// a newly instantiated collection can't have orphans
584585
&& ! ( (PersistentCollection<?>) child ).isNewlyInstantiated();

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import org.hibernate.persister.entity.EntityPersister;
1818
import org.hibernate.proxy.HibernateProxy;
1919
import org.hibernate.proxy.LazyInitializer;
20-
import org.hibernate.type.CompositeType;
20+
import org.hibernate.type.AnyType;
21+
import org.hibernate.type.ComponentType;
2122
import org.hibernate.type.EntityType;
2223
import org.hibernate.type.Type;
2324

@@ -91,7 +92,7 @@ private Object nullifyTransientReferences(final Object value, final String prope
9192
if ( value == null ) {
9293
returnedValue = null;
9394
}
94-
else if ( type.isEntityType() ) {
95+
else if ( type instanceof EntityType ) {
9596
final EntityType entityType = (EntityType) type;
9697
if ( entityType.isOneToOne() ) {
9798
returnedValue = value;
@@ -113,11 +114,11 @@ else if ( type.isEntityType() ) {
113114
}
114115
}
115116
}
116-
else if ( type.isAnyType() ) {
117+
else if ( type instanceof AnyType ) {
117118
returnedValue = isNullifiable( null, value ) ? null : value;
118119
}
119-
else if ( type.isComponentType() ) {
120-
final CompositeType actype = (CompositeType) type;
120+
else if ( type instanceof ComponentType ) {
121+
final ComponentType actype = (ComponentType) type;
121122
final Object[] subvalues = actype.getPropertyValues( value, session );
122123
final Type[] subtypes = actype.getSubtypes();
123124
final String[] subPropertyNames = actype.getPropertyNames();
@@ -159,7 +160,7 @@ private Object initializeIfNecessary(
159160
final Type type) {
160161
if ( isDelete &&
161162
value == LazyPropertyInitializer.UNFETCHED_PROPERTY &&
162-
type.isEntityType() &&
163+
type instanceof EntityType &&
163164
!session.getPersistenceContextInternal().isNullifiableEntityKeysEmpty() ) {
164165
// IMPLEMENTATION NOTE: If cascade-remove was mapped for the attribute,
165166
// then value should have been initialized previously, when the remove operation was
@@ -406,21 +407,21 @@ private static void collectNonNullableTransientEntities(
406407
return;
407408
}
408409

409-
if ( type.isEntityType() ) {
410+
if ( type instanceof EntityType ) {
410411
final EntityType entityType = (EntityType) type;
411412
if ( !isNullable
412413
&& !entityType.isOneToOne()
413414
&& nullifier.isNullifiable( entityType.getAssociatedEntityName(), value ) ) {
414415
nonNullableTransientEntities.add( propertyName, value );
415416
}
416417
}
417-
else if ( type.isAnyType() ) {
418+
else if ( type instanceof AnyType ) {
418419
if ( !isNullable && nullifier.isNullifiable( null, value ) ) {
419420
nonNullableTransientEntities.add( propertyName, value );
420421
}
421422
}
422-
else if ( type.isComponentType() ) {
423-
final CompositeType actype = (CompositeType) type;
423+
else if ( type instanceof ComponentType ) {
424+
final ComponentType actype = (ComponentType) type;
424425
final boolean[] subValueNullability = actype.getPropertyNullability();
425426
if ( subValueNullability != null ) {
426427
final String[] subPropertyNames = actype.getPropertyNames();

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1616
import org.hibernate.persister.entity.EntityPersister;
1717
import org.hibernate.generator.Generator;
18+
import org.hibernate.type.AnyType;
1819
import org.hibernate.type.CollectionType;
20+
import org.hibernate.type.ComponentType;
1921
import org.hibernate.type.CompositeType;
2022
import org.hibernate.type.Type;
2123

@@ -143,16 +145,19 @@ private static boolean generated(Generator generator) {
143145
* @throws HibernateException error while getting subcomponent values
144146
*/
145147
private String checkSubElementsNullability(Type propertyType, Object value) throws HibernateException {
146-
if ( propertyType.isComponentType() ) {
147-
return checkComponentNullability( value, (CompositeType) propertyType );
148+
if ( propertyType instanceof AnyType ) {
149+
return checkComponentNullability( value, (AnyType) propertyType );
150+
}
151+
if ( propertyType instanceof ComponentType ) {
152+
return checkComponentNullability( value, (ComponentType) propertyType );
148153
}
149154

150-
if ( propertyType.isCollectionType() ) {
155+
if ( propertyType instanceof CollectionType ) {
151156
// persistent collections may have components
152157
final CollectionType collectionType = (CollectionType) propertyType;
153158
final Type collectionElementType = collectionType.getElementType( session.getFactory() );
154159

155-
if ( collectionElementType.isComponentType() ) {
160+
if ( collectionElementType instanceof ComponentType || collectionElementType instanceof AnyType ) {
156161
// check for all components values in the collection
157162
final CompositeType componentType = (CompositeType) collectionElementType;
158163
final Iterator<?> itr = CascadingActions.getLoadedElementsIterator( session, collectionType, value );
@@ -188,7 +193,7 @@ private String checkComponentNullability(Object value, CompositeType compositeTy
188193
//
189194
// The more correct fix would be to cascade saves of the many-to-any elements before the Nullability checking
190195

191-
if ( compositeType.isAnyType() ) {
196+
if ( compositeType instanceof AnyType ) {
192197
return null;
193198
}
194199

hibernate-core/src/main/java/org/hibernate/engine/profile/FetchProfile.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.hibernate.internal.CoreLogging;
1313
import org.hibernate.internal.CoreMessageLogger;
1414
import org.hibernate.type.BagType;
15+
import org.hibernate.type.CollectionType;
1516
import org.hibernate.type.Type;
1617

1718
/**
@@ -82,7 +83,7 @@ public void addFetch(Association association, Fetch.Style style) {
8283
public void addFetch(final Fetch fetch) {
8384
final String fetchAssociactionRole = fetch.getAssociation().getRole();
8485
final Type associationType = fetch.getAssociation().getOwner().getPropertyType( fetch.getAssociation().getAssociationPath() );
85-
if ( associationType.isCollectionType() ) {
86+
if ( associationType instanceof CollectionType ) {
8687
LOG.tracev( "Handling request to add collection fetch [{0}]", fetchAssociactionRole );
8788

8889
// couple of things for which to account in the case of collection

hibernate-core/src/main/java/org/hibernate/engine/spi/ActionQueue.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import org.hibernate.proxy.HibernateProxy;
5454
import org.hibernate.proxy.LazyInitializer;
5555
import org.hibernate.type.CollectionType;
56-
import org.hibernate.type.CompositeType;
56+
import org.hibernate.type.ComponentType;
5757
import org.hibernate.type.EntityType;
5858
import org.hibernate.type.ForeignKeyDirection;
5959
import org.hibernate.type.OneToOneType;
@@ -1151,7 +1151,10 @@ public void addTransitiveDependencies(InsertInfo origin, Set<InsertInfo> visited
11511151
}
11521152

11531153
private void addDirectDependency(Type type, Object value, IdentityHashMap<Object, InsertInfo> insertInfosByEntity) {
1154-
if ( type.isEntityType() && value != null ) {
1154+
if ( value == null ) {
1155+
return;
1156+
}
1157+
if ( type instanceof EntityType ) {
11551158
final EntityType entityType = (EntityType) type;
11561159
final InsertInfo insertInfo = insertInfosByEntity.get(value);
11571160
if (insertInfo != null) {
@@ -1171,7 +1174,7 @@ private void addDirectDependency(Type type, Object value, IdentityHashMap<Object
11711174
}
11721175
}
11731176
}
1174-
else if ( type.isCollectionType() && value != null ) {
1177+
else if ( type instanceof CollectionType ) {
11751178
CollectionType collectionType = (CollectionType) type;
11761179
final PluralAttributeMapping pluralAttributeMapping = insertAction.getSession()
11771180
.getFactory()
@@ -1194,9 +1197,9 @@ else if ( type.isCollectionType() && value != null ) {
11941197
}
11951198
}
11961199
}
1197-
else if ( type.isComponentType() && value != null ) {
1200+
else if ( type instanceof ComponentType ) {
11981201
// Support recursive checks of composite type properties for associations and collections.
1199-
CompositeType compositeType = (CompositeType) type;
1202+
ComponentType compositeType = (ComponentType) type;
12001203
final SharedSessionContractImplementor session = insertAction.getSession();
12011204
Object[] componentValues = compositeType.getPropertyValues( value, session );
12021205
for ( int j = 0; j < componentValues.length; ++j ) {

hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingActions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public void noCascade(
359359
EntityPersister persister,
360360
Type propertyType,
361361
int propertyIndex) {
362-
if ( propertyType.isEntityType() ) {
362+
if ( propertyType instanceof EntityType ) {
363363
Object child = persister.getValue( parent, propertyIndex );
364364
if ( child != null
365365
&& !isInManagedState( child, session )

hibernate-core/src/main/java/org/hibernate/event/internal/AbstractVisitor.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
1111
import org.hibernate.event.spi.EventSource;
1212
import org.hibernate.persister.entity.EntityPersister;
13+
import org.hibernate.type.AnyType;
1314
import org.hibernate.type.CollectionType;
15+
import org.hibernate.type.ComponentType;
1416
import org.hibernate.type.CompositeType;
1517
import org.hibernate.type.EntityType;
1618
import org.hibernate.type.Type;
@@ -87,15 +89,18 @@ Object processComponent(Object component, CompositeType componentType) throws Hi
8789
*/
8890
final Object processValue(Object value, Type type) throws HibernateException {
8991

90-
if ( type.isCollectionType() ) {
92+
if ( type instanceof CollectionType ) {
9193
//even process null collections
9294
return processCollection( value, (CollectionType) type );
9395
}
94-
else if ( type.isEntityType() ) {
96+
else if ( type instanceof EntityType ) {
9597
return processEntity( value, (EntityType) type );
9698
}
97-
else if ( type.isComponentType() ) {
98-
return processComponent( value, (CompositeType) type );
99+
else if ( type instanceof ComponentType ) {
100+
return processComponent( value, (ComponentType) type );
101+
}
102+
else if ( type instanceof AnyType ) {
103+
return processComponent( value, (AnyType) type );
99104
}
100105
else {
101106
return null;

0 commit comments

Comments
 (0)