Skip to content

Commit 059e763

Browse files
committed
miscellaneous minor code cleanups
1 parent eacb836 commit 059e763

File tree

6 files changed

+198
-241
lines changed

6 files changed

+198
-241
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3239,42 +3239,49 @@ private void bindManyToManyFilters(
32393239
}
32403240

32413241
for ( var filterSource : elementSource.getFilterSources() ) {
3242-
if ( filterSource.getName() == null ) {
3243-
if ( BOOT_LOGGER.isTraceEnabled() ) {
3244-
BOOT_LOGGER.tracef(
3245-
"Encountered filter with no name associated with many-to-many [%s]; skipping",
3246-
getPluralAttributeSource().getAttributeRole().getFullPath()
3247-
);
3248-
}
3242+
bindManyToManyFilter( mappingDocument, collectionBinding, filterSource );
3243+
}
3244+
}
3245+
3246+
private void bindManyToManyFilter(
3247+
MappingDocument mappingDocument, Collection collectionBinding, FilterSource filterSource) {
3248+
final String name = filterSource.getName();
3249+
if ( name == null ) {
3250+
if ( BOOT_LOGGER.isTraceEnabled() ) {
3251+
BOOT_LOGGER.tracef(
3252+
"Encountered filter with no name associated with many-to-many [%s]; skipping",
3253+
getPluralAttributeSource().getAttributeRole().getFullPath()
3254+
);
32493255
}
3250-
else {
3251-
if ( filterSource.getCondition() == null ) {
3252-
throw new MappingException(
3253-
String.format(
3254-
Locale.ENGLISH,
3255-
"No filter condition found for filter [%s] associated with many-to-many [%s]",
3256-
filterSource.getName(),
3257-
getPluralAttributeSource().getAttributeRole().getFullPath()
3258-
),
3259-
mappingDocument.getOrigin()
3260-
);
3261-
}
3262-
if ( BOOT_LOGGER.isTraceEnabled() ) {
3263-
BOOT_LOGGER.tracef(
3264-
"Applying many-to-many filter [%s] as [%s] to collection [%s]",
3265-
filterSource.getName(),
3266-
filterSource.getCondition(),
3267-
getPluralAttributeSource().getAttributeRole().getFullPath()
3268-
);
3269-
}
3270-
collectionBinding.addManyToManyFilter(
3271-
filterSource.getName(),
3272-
filterSource.getCondition(),
3273-
filterSource.shouldAutoInjectAliases(),
3274-
filterSource.getAliasToTableMap(),
3275-
filterSource.getAliasToEntityMap()
3256+
}
3257+
else {
3258+
final String condition = filterSource.getCondition();
3259+
if ( condition == null ) {
3260+
throw new MappingException(
3261+
String.format(
3262+
Locale.ENGLISH,
3263+
"No filter condition found for filter [%s] associated with many-to-many [%s]",
3264+
name,
3265+
getPluralAttributeSource().getAttributeRole().getFullPath()
3266+
),
3267+
mappingDocument.getOrigin()
32763268
);
32773269
}
3270+
if ( BOOT_LOGGER.isTraceEnabled() ) {
3271+
BOOT_LOGGER.tracef(
3272+
"Applying many-to-many filter [%s] as [%s] to collection [%s]",
3273+
name,
3274+
condition,
3275+
getPluralAttributeSource().getAttributeRole().getFullPath()
3276+
);
3277+
}
3278+
collectionBinding.addManyToManyFilter(
3279+
name,
3280+
condition,
3281+
filterSource.shouldAutoInjectAliases(),
3282+
filterSource.getAliasToTableMap(),
3283+
filterSource.getAliasToEntityMap()
3284+
);
32783285
}
32793286
}
32803287

@@ -3451,10 +3458,9 @@ protected void createBackReferences() {
34513458
&& !collectionBinding.isInverse()
34523459
&& !indexIsFormula ) {
34533460
final var oneToMany = (OneToMany) collectionBinding.getElement();
3454-
final String entityName = oneToMany.getReferencedEntityName();
34553461
final var referenced =
34563462
getMappingDocument().getMetadataCollector()
3457-
.getEntityBinding( entityName );
3463+
.getEntityBinding( oneToMany.getReferencedEntityName() );
34583464
final var backref = new IndexBackref();
34593465
backref.setName( '_' + collectionBinding.getOwnerEntityName()
34603466
+ "." + getPluralAttributeSource().getName() + "IndexBackref" );
@@ -3533,10 +3539,9 @@ private void createIndexBackRef(
35333539
&& !collectionBinding.getKey().isNullable()
35343540
&& !collectionBinding.isInverse() ) {
35353541
final var oneToMany = (OneToMany) collectionBinding.getElement();
3536-
final String entityName = oneToMany.getReferencedEntityName();
35373542
final var referenced =
35383543
mappingDocument.getMetadataCollector()
3539-
.getEntityBinding( entityName );
3544+
.getEntityBinding( oneToMany.getReferencedEntityName() );
35403545
final var backref = new IndexBackref();
35413546
backref.setName( '_' + collectionBinding.getOwnerEntityName()
35423547
+ "." + pluralAttributeSource.getName() + "IndexBackref" );

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

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,26 @@ public interface InheritanceChecker {
3232
* Should the given property be included in the owner's base fetch group?
3333
*/
3434
public static boolean includeInBaseFetchGroup(
35-
Property bootMapping,
35+
Property property,
3636
boolean isEnhanced,
3737
InheritanceChecker inheritanceChecker,
3838
boolean collectionsInDefaultFetchGroupEnabled) {
39-
final var value = bootMapping.getValue();
40-
41-
if ( ! isEnhanced ) {
42-
if ( value instanceof ToOne toOne ) {
43-
if ( toOne.isUnwrapProxy() ) {
44-
BYTECODE_INTERCEPTOR_LOGGER.toOneLazyNoProxyButNotEnhanced(
45-
bootMapping.getPersistentClass().getEntityName(),
46-
bootMapping.getName()
47-
);
48-
}
39+
final var value = property.getValue();
40+
41+
if ( !isEnhanced ) {
42+
if ( value instanceof ToOne toOne && toOne.isUnwrapProxy() ) {
43+
BYTECODE_INTERCEPTOR_LOGGER.toOneLazyNoProxyButNotEnhanced(
44+
property.getPersistentClass().getEntityName(),
45+
property.getName()
46+
);
4947
}
5048
return true;
5149
}
5250

5351
// if we get here, we know the property owner is enhanced for laziness
5452
//
55-
// NOTE : we make the (potentially untrue) assumption here that
56-
// if the owner is enhanced, then all classes are enhanced..
53+
// NOTE: we make the (potentially untrue) assumption here that
54+
// if the owner is enhanced, then all classes are enhanced.
5755

5856
if ( value instanceof ToOne toOne ) {
5957

@@ -64,17 +62,17 @@ public static boolean includeInBaseFetchGroup(
6462

6563
// it is lazy. see if we should select the FK
6664

67-
if ( bootMapping.getLazyGroup() != null ) {
65+
if ( property.getLazyGroup() != null ) {
6866
// a non-base fetch group was explicitly specified
6967
//
7068
// really this should indicate to not select it as part of the base group.
7169
// however, at the time being that leads to inefficient SQL - so for now
7270
// we simply log a message that we are ignoring the `@LazyGroup` for to-ones
7371

7472
BYTECODE_INTERCEPTOR_LOGGER.lazyGroupIgnoredForToOne(
75-
bootMapping.getLazyGroup(),
76-
bootMapping.getPersistentClass().getEntityName(),
77-
bootMapping.getName()
73+
property.getLazyGroup(),
74+
property.getPersistentClass().getEntityName(),
75+
property.getName()
7876
);
7977

8078
// at a later time - for example 6.0 when we can implement the join solution
@@ -84,29 +82,30 @@ public static boolean includeInBaseFetchGroup(
8482
// for now, fall through
8583
}
8684

87-
if ( ! toOne.isReferenceToPrimaryKey() ) {
85+
if ( !toOne.isReferenceToPrimaryKey() ) {
8886
// we do not have a reference to the associated primary-key
8987
return false;
9088
}
9189

9290
if ( toOne.getColumnSpan() == 0 ) {
93-
// generally this would indicate a "shared PK" on-to-one and there
91+
// generally this would indicate a "shared PK" one-to-one and there
9492
// is no column for the association on the owner table - do not add
9593
// the association to the base group (which would force an immediate
9694
// select from the association table effectively making this
9795
// association non-lazy)
9896
return false;
9997
}
10098

101-
final boolean unwrapExplicitlyRequested = toOne.isUnwrapProxy() && !toOne.isUnwrapProxyImplicit();
99+
final boolean unwrapExplicitlyRequested =
100+
toOne.isUnwrapProxy() && !toOne.isUnwrapProxyImplicit();
102101

103102
if ( inheritanceChecker.hasSubclasses( toOne.getReferencedEntityName() ) ) {
104103
// the associated type has subclasses - we cannot use the enhanced proxy and will generate a HibernateProxy
105104
if ( unwrapExplicitlyRequested ) {
106105
// NO_PROXY was explicitly requested
107106
BYTECODE_INTERCEPTOR_LOGGER.lazyNoProxyButAssociatedHasSubclasses(
108-
bootMapping.getPersistentClass().getEntityName(),
109-
bootMapping.getName(),
107+
property.getPersistentClass().getEntityName(),
108+
property.getName(),
110109
toOne.getReferencedEntityName()
111110
);
112111
}
@@ -117,8 +116,8 @@ public static boolean includeInBaseFetchGroup(
117116
if ( toOne instanceof ManyToOne manyToOne && manyToOne.isIgnoreNotFound() ) {
118117
if ( unwrapExplicitlyRequested ) {
119118
BYTECODE_INTERCEPTOR_LOGGER.notFoundIgnoreWithNoProxySkippingFkSelection(
120-
bootMapping.getPersistentClass().getEntityName(),
121-
bootMapping.getName()
119+
property.getPersistentClass().getEntityName(),
120+
property.getName()
122121
);
123122
return false;
124123
}
@@ -135,15 +134,15 @@ public static boolean includeInBaseFetchGroup(
135134
}
136135

137136
return collectionsInDefaultFetchGroupEnabled && ( value instanceof Collection )
138-
|| ! bootMapping.isLazy();
137+
|| ! property.isLazy();
139138
}
140139

141140
public static <T> T performWork(
142141
BytecodeLazyAttributeInterceptor interceptor,
143142
BiFunction<SharedSessionContractImplementor, Boolean, T> work,
144143
String entityName,
145144
String attributeName) {
146-
SharedSessionContractImplementor session = interceptor.getLinkedSession();
145+
var session = interceptor.getLinkedSession();
147146

148147
final boolean isTempSession;
149148
final boolean isJta;
@@ -236,23 +235,20 @@ enum Cause {
236235
NO_SF_UUID
237236
}
238237

239-
private static LazyInitializationException createLazyInitializationException(final Cause cause, final String entityName, final String attributeName) {
240-
final String reason = switch ( cause ) {
241-
case NO_SESSION -> "no session and settings disallow loading outside the Session";
242-
case CLOSED_SESSION -> "session is closed and settings disallow loading outside the Session";
243-
case DISCONNECTED_SESSION -> "session is disconnected and settings disallow loading outside the Session";
244-
case NO_SF_UUID -> "could not determine SessionFactory UUId to create temporary Session for loading";
245-
};
246-
247-
final String message = String.format(
238+
private static LazyInitializationException createLazyInitializationException(
239+
Cause cause, String entityName, String attributeName) {
240+
return new LazyInitializationException( String.format(
248241
Locale.ROOT,
249242
"Unable to perform requested lazy initialization [%s.%s] - %s",
250243
entityName,
251244
attributeName,
252-
reason
253-
);
254-
255-
return new LazyInitializationException( message );
245+
switch ( cause ) {
246+
case NO_SESSION -> "no session and settings disallow loading outside the Session";
247+
case CLOSED_SESSION -> "session is closed and settings disallow loading outside the Session";
248+
case DISCONNECTED_SESSION -> "session is disconnected and settings disallow loading outside the Session";
249+
case NO_SF_UUID -> "could not determine SessionFactory UUId to create temporary Session for loading";
250+
}
251+
) );
256252
}
257253

258254
private static SharedSessionContractImplementor openTemporarySessionForLoading(

hibernate-core/src/main/java/org/hibernate/bytecode/internal/BytecodeEnhancementMetadataPojoImpl.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,14 @@
1414
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributesMetadata;
1515
import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata;
1616
import org.hibernate.bytecode.spi.NotInstrumentedException;
17-
import org.hibernate.engine.spi.EntityEntry;
18-
import org.hibernate.engine.spi.EntityHolder;
1917
import org.hibernate.engine.spi.EntityKey;
2018
import org.hibernate.engine.spi.ManagedEntity;
21-
import org.hibernate.engine.spi.PersistenceContext;
2219
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
2320
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
2421
import org.hibernate.engine.spi.SelfDirtinessTracker;
2522
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2623
import org.hibernate.engine.spi.Status;
2724
import org.hibernate.mapping.PersistentClass;
28-
import org.hibernate.persister.entity.EntityPersister;
2925
import org.hibernate.type.CompositeType;
3026

3127
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -50,9 +46,9 @@ public static BytecodeEnhancementMetadataPojoImpl from(
5046
CompositeType nonAggregatedCidMapper,
5147
boolean collectionsInDefaultFetchGroupEnabled,
5248
Metadata metadata) {
53-
final Class<?> mappedClass = persistentClass.getMappedClass();
49+
final var mappedClass = persistentClass.getMappedClass();
5450
final boolean enhancedForLazyLoading = isPersistentAttributeInterceptableType( mappedClass );
55-
final LazyAttributesMetadata lazyAttributesMetadata = enhancedForLazyLoading
51+
final var lazyAttributesMetadata = enhancedForLazyLoading
5652
? LazyAttributesMetadata.from( persistentClass, true, collectionsInDefaultFetchGroupEnabled, metadata )
5753
: LazyAttributesMetadata.nonEnhanced( persistentClass.getEntityName() );
5854

@@ -139,7 +135,7 @@ public boolean isAttributeLoaded(Object entity, String attributeName) {
139135
return true;
140136
}
141137

142-
final BytecodeLazyAttributeInterceptor interceptor = extractLazyInterceptor( entity );
138+
final var interceptor = extractLazyInterceptor( entity );
143139
if ( interceptor instanceof LazyAttributeLoadingInterceptor ) {
144140
return interceptor.isAttributeLoaded( attributeName );
145141
}
@@ -154,12 +150,12 @@ public boolean isAttributeLoaded(Object entity, String attributeName) {
154150

155151
@Override
156152
public PersistentAttributeInterceptable createEnhancedProxy(EntityKey entityKey, boolean addEmptyEntry, SharedSessionContractImplementor session) {
157-
final EntityPersister persister = entityKey.getPersister();
153+
final var persister = entityKey.getPersister();
158154
final Object identifier = entityKey.getIdentifier();
159-
final PersistenceContext persistenceContext = session.getPersistenceContext();
155+
final var persistenceContext = session.getPersistenceContext();
160156

161157
// first, instantiate the entity instance to use as the proxy
162-
final PersistentAttributeInterceptable entity = asPersistentAttributeInterceptable( persister.instantiate( identifier, session ) );
158+
final var entity = asPersistentAttributeInterceptable( persister.instantiate( identifier, session ) );
163159

164160
// clear the fields that are marked as dirty in the dirtiness tracker
165161
processIfSelfDirtinessTracker( entity, BytecodeEnhancementMetadataPojoImpl::clearDirtyAttributes );
@@ -170,8 +166,8 @@ public PersistentAttributeInterceptable createEnhancedProxy(EntityKey entityKey,
170166

171167
// if requested, add the "holder entry" to the PC
172168
if ( addEmptyEntry ) {
173-
EntityHolder entityHolder = persistenceContext.getEntityHolder( entityKey );
174-
EntityEntry entityEntry = persistenceContext.addEntry(
169+
final var entityHolder = persistenceContext.getEntityHolder( entityKey );
170+
final var entityEntry = persistenceContext.addEntry(
175171
entity,
176172
Status.MANAGED,
177173
// loaded state
@@ -223,14 +219,12 @@ public LazyAttributeLoadingInterceptor injectInterceptor(
223219
)
224220
);
225221
}
226-
final LazyAttributeLoadingInterceptor interceptor = new LazyAttributeLoadingInterceptor(
227-
this.lazyAttributeLoadingInterceptorState,
222+
final var interceptor = new LazyAttributeLoadingInterceptor(
223+
lazyAttributeLoadingInterceptorState,
228224
identifier,
229225
session
230226
);
231-
232227
injectInterceptor( entity, interceptor, session );
233-
234228
return interceptor;
235229
}
236230

@@ -257,10 +251,11 @@ public void injectEnhancedEntityAsProxyInterceptor(
257251
//This state object needs to be lazily initialized as it needs access to the Persister, but once
258252
//initialized it can be reused across multiple sessions.
259253
public EnhancementAsProxyLazinessInterceptor.EntityRelatedState getEnhancementAsProxyLazinessInterceptorMetastate(SharedSessionContractImplementor session) {
260-
EnhancementAsProxyLazinessInterceptor.EntityRelatedState state = this.enhancementAsProxyInterceptorState;
254+
var state = this.enhancementAsProxyInterceptorState;
261255
if ( state == null ) {
262-
final EntityPersister entityPersister = session.getFactory().getMappingMetamodel()
263-
.getEntityDescriptor( entityName );
256+
final var entityPersister =
257+
session.getFactory().getMappingMetamodel()
258+
.getEntityDescriptor( entityName );
264259
state = new EnhancementAsProxyLazinessInterceptor.EntityRelatedState(
265260
entityPersister,
266261
nonAggregatedCidMapper,

0 commit comments

Comments
 (0)