Skip to content

Commit 809c5d7

Browse files
committed
some minor refactorings to Initializers
1 parent 10f4e24 commit 809c5d7

File tree

3 files changed

+177
-192
lines changed

3 files changed

+177
-192
lines changed

hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/AbstractBatchEntitySelectFetchInitializer.java

Lines changed: 107 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
1212
import org.hibernate.engine.spi.EntityHolder;
1313
import org.hibernate.engine.spi.EntityKey;
14-
import org.hibernate.engine.spi.PersistenceContext;
1514
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1615
import org.hibernate.metamodel.mapping.AttributeMapping;
1716
import org.hibernate.metamodel.mapping.EntityMappingType;
1817
import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping;
1918
import org.hibernate.persister.entity.EntityPersister;
20-
import org.hibernate.proxy.HibernateProxy;
2119
import org.hibernate.proxy.LazyInitializer;
2220
import org.hibernate.spi.NavigablePath;
2321
import org.hibernate.sql.results.graph.AssemblerCreationState;
@@ -39,7 +37,8 @@ public abstract class AbstractBatchEntitySelectFetchInitializer<Data extends Abs
3937

4038
protected final EntityInitializer<InitializerData> owningEntityInitializer;
4139

42-
public static abstract class AbstractBatchEntitySelectFetchInitializerData extends EntitySelectFetchInitializerData {
40+
public static abstract class AbstractBatchEntitySelectFetchInitializerData
41+
extends EntitySelectFetchInitializerData {
4342
final boolean batchDisabled;
4443

4544
// per-row state
@@ -48,10 +47,11 @@ public static abstract class AbstractBatchEntitySelectFetchInitializerData exten
4847
public AbstractBatchEntitySelectFetchInitializerData(
4948
AbstractBatchEntitySelectFetchInitializer<?> initializer,
5049
RowProcessingState rowProcessingState) {
51-
super( initializer, rowProcessingState );
50+
super( rowProcessingState );
5251

5352
batchDisabled = rowProcessingState.isScrollResult()
54-
|| !rowProcessingState.getLoadQueryInfluencers().effectivelyBatchLoadable( initializer.toOneMapping.getEntityMappingType().getEntityPersister() );
53+
|| !rowProcessingState.getLoadQueryInfluencers()
54+
.effectivelyBatchLoadable( initializer.toOneMapping.getEntityMappingType().getEntityPersister() );
5555
}
5656
}
5757

@@ -73,51 +73,47 @@ public AbstractBatchEntitySelectFetchInitializer(
7373

7474
@Override
7575
public void resolveKey(Data data) {
76-
if ( data.getState() != State.UNINITIALIZED ) {
77-
return;
78-
}
79-
80-
data.entityKey = null;
81-
data.setInstance( null );
82-
final RowProcessingState rowProcessingState = data.getRowProcessingState();
83-
//noinspection unchecked
84-
final Initializer<InitializerData> initializer = (Initializer<InitializerData>) keyAssembler.getInitializer();
85-
if ( initializer != null ) {
86-
final InitializerData subData = initializer.getData( rowProcessingState );
87-
initializer.resolveKey( subData );
88-
data.entityIdentifier = null;
89-
data.setState( subData.getState() == State.MISSING ? State.MISSING : State.KEY_RESOLVED );
90-
}
91-
else {
92-
data.entityIdentifier = keyAssembler.assemble( rowProcessingState );
93-
data.setState( data.entityIdentifier == null ? State.MISSING : State.KEY_RESOLVED );
76+
if ( data.getState() == State.UNINITIALIZED ) {
77+
data.entityKey = null;
78+
data.setInstance( null );
79+
final RowProcessingState rowProcessingState = data.getRowProcessingState();
80+
//noinspection unchecked
81+
final Initializer<InitializerData> initializer = (Initializer<InitializerData>) keyAssembler.getInitializer();
82+
if ( initializer != null ) {
83+
final InitializerData subData = initializer.getData( rowProcessingState );
84+
initializer.resolveKey( subData );
85+
data.entityIdentifier = null;
86+
data.setState( subData.getState() == State.MISSING ? State.MISSING : State.KEY_RESOLVED );
87+
}
88+
else {
89+
data.entityIdentifier = keyAssembler.assemble( rowProcessingState );
90+
data.setState( data.entityIdentifier == null ? State.MISSING : State.KEY_RESOLVED );
91+
}
9492
}
9593
}
9694

9795
@Override
9896
public void resolveInstance(Data data) {
99-
if ( data.getState() != State.KEY_RESOLVED ) {
100-
return;
101-
}
102-
103-
data.setState( State.RESOLVED );
104-
final RowProcessingState rowProcessingState = data.getRowProcessingState();
105-
if ( data.entityIdentifier == null ) {
106-
// entityIdentifier can be null if the identifier is based on an initializer
107-
data.entityIdentifier = keyAssembler.assemble( rowProcessingState );
97+
if ( data.getState() == State.KEY_RESOLVED ) {
98+
data.setState( State.RESOLVED );
99+
final RowProcessingState rowProcessingState = data.getRowProcessingState();
108100
if ( data.entityIdentifier == null ) {
109-
data.entityKey = null;
110-
data.setInstance( null );
111-
data.setState( State.MISSING );
112-
return;
101+
// entityIdentifier can be null if the identifier is based on an initializer
102+
data.entityIdentifier = keyAssembler.assemble( rowProcessingState );
103+
if ( data.entityIdentifier == null ) {
104+
data.entityKey = null;
105+
data.setInstance( null );
106+
data.setState( State.MISSING );
107+
return;
108+
}
113109
}
110+
resolveInstanceFromIdentifier( data );
114111
}
115-
resolveInstanceFromIdentifier( data );
116112
}
117113

118114
protected void resolveInstanceFromIdentifier(Data data) {
119115
if ( data.batchDisabled ) {
120-
initialize( data );
116+
initializeIfNecessary( data );
121117
}
122118
else {
123119
data.entityKey = new EntityKey( data.entityIdentifier, concreteDescriptor );
@@ -136,87 +132,92 @@ public void resolveInstance(Object instance, Data data) {
136132
data.setState( State.MISSING );
137133
data.entityKey = null;
138134
data.setInstance( null );
139-
return;
140135
}
141-
final RowProcessingState rowProcessingState = data.getRowProcessingState();
142-
// Only need to extract the identifier if the identifier has a many to one
143-
final LazyInitializer lazyInitializer = extractLazyInitializer( instance );
144-
data.entityKey = null;
145-
data.entityIdentifier = null;
146-
if ( lazyInitializer == null ) {
147-
// Entity is most probably initialized
148-
data.setInstance( instance );
149-
if ( concreteDescriptor.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()
150-
&& isPersistentAttributeInterceptable( instance )
151-
&& getAttributeInterceptor( instance )
152-
instanceof EnhancementAsProxyLazinessInterceptor enhancementInterceptor ) {
153-
if ( enhancementInterceptor.isInitialized() ) {
154-
data.setState( State.INITIALIZED );
136+
else {
137+
final RowProcessingState rowProcessingState = data.getRowProcessingState();
138+
// Only need to extract the identifier if the identifier has a many to one
139+
final LazyInitializer lazyInitializer = extractLazyInitializer( instance );
140+
data.entityKey = null;
141+
data.entityIdentifier = null;
142+
if ( lazyInitializer == null ) {
143+
// Entity is most probably initialized
144+
data.setInstance( instance );
145+
if ( concreteDescriptor.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()
146+
&& isPersistentAttributeInterceptable( instance )
147+
&& getAttributeInterceptor( instance )
148+
instanceof EnhancementAsProxyLazinessInterceptor enhancementInterceptor ) {
149+
if ( enhancementInterceptor.isInitialized() ) {
150+
data.setState( State.INITIALIZED );
151+
}
152+
else {
153+
data.setState( State.RESOLVED );
154+
data.entityIdentifier = enhancementInterceptor.getIdentifier();
155+
}
156+
if ( keyIsEager && data.entityIdentifier == null ) {
157+
data.entityIdentifier =
158+
concreteDescriptor.getIdentifier( instance, rowProcessingState.getSession() );
159+
}
155160
}
156161
else {
162+
// If the entity initializer is null, we know the entity is fully initialized,
163+
// otherwise it will be initialized by some other initializer
157164
data.setState( State.RESOLVED );
158-
data.entityIdentifier = enhancementInterceptor.getIdentifier();
165+
data.entityIdentifier =
166+
concreteDescriptor.getIdentifier( instance, rowProcessingState.getSession() );
159167
}
160168
}
161-
else {
162-
// If the entity initializer is null, we know the entity is fully initialized,
163-
// otherwise it will be initialized by some other initializer
169+
else if ( lazyInitializer.isUninitialized() ) {
164170
data.setState( State.RESOLVED );
165-
data.entityIdentifier = concreteDescriptor.getIdentifier( instance, rowProcessingState.getSession() );
171+
data.entityIdentifier = lazyInitializer.getInternalIdentifier();
172+
}
173+
else {
174+
// Entity is initialized
175+
data.setState( State.INITIALIZED );
176+
if ( keyIsEager ) {
177+
data.entityIdentifier = lazyInitializer.getInternalIdentifier();
178+
}
179+
data.setInstance( lazyInitializer.getImplementation() );
166180
}
167-
if ( keyIsEager && data.entityIdentifier == null ) {
168-
data.entityIdentifier = concreteDescriptor.getIdentifier( instance, rowProcessingState.getSession() );
181+
182+
if ( data.getState() == State.RESOLVED ) {
183+
resolveInstanceFromIdentifier( data );
169184
}
170-
}
171-
else if ( lazyInitializer.isUninitialized() ) {
172-
data.setState( State.RESOLVED );
173-
data.entityIdentifier = lazyInitializer.getInternalIdentifier();
174-
}
175-
else {
176-
// Entity is initialized
177-
data.setState( State.INITIALIZED );
178185
if ( keyIsEager ) {
179-
data.entityIdentifier = lazyInitializer.getInternalIdentifier();
186+
final Initializer<?> initializer = keyAssembler.getInitializer();
187+
assert initializer != null;
188+
initializer.resolveInstance( data.entityIdentifier, rowProcessingState );
189+
}
190+
else if ( rowProcessingState.needsResolveState() ) {
191+
// Resolve the state of the identifier if result caching is enabled and this is not a query cache hit
192+
keyAssembler.resolveState( rowProcessingState );
180193
}
181-
data.setInstance( lazyInitializer.getImplementation() );
182-
}
183-
184-
if ( data.getState() == State.RESOLVED ) {
185-
resolveInstanceFromIdentifier( data );
186-
}
187-
if ( keyIsEager ) {
188-
final Initializer<?> initializer = keyAssembler.getInitializer();
189-
assert initializer != null;
190-
initializer.resolveInstance( data.entityIdentifier, rowProcessingState );
191-
}
192-
else if ( rowProcessingState.needsResolveState() ) {
193-
// Resolve the state of the identifier if result caching is enabled and this is not a query cache hit
194-
keyAssembler.resolveState( rowProcessingState );
195194
}
196195
}
197196

198197
@Override
199198
public void initializeInstance(Data data) {
200-
if ( data.getState() != State.RESOLVED ) {
201-
return;
202-
}
203-
data.setState( State.INITIALIZED );
204-
if ( data.batchDisabled ) {
205-
Hibernate.initialize( data.getInstance() );
199+
if ( data.getState() == State.RESOLVED ) {
200+
data.setState( State.INITIALIZED );
201+
if ( data.batchDisabled ) {
202+
Hibernate.initialize( data.getInstance() );
203+
}
206204
}
207205
}
208206

209207
protected Object getExistingInitializedInstance(Data data) {
210-
final SharedSessionContractImplementor session = data.getRowProcessingState().getSession();
211-
final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
212-
final EntityHolder holder = persistenceContext.getEntityHolder( data.entityKey );
208+
final EntityHolder holder =
209+
data.getRowProcessingState().getSession()
210+
.getPersistenceContextInternal()
211+
.getEntityHolder( data.entityKey );
213212
if ( holder != null && holder.getEntity() != null && holder.isEventuallyInitialized() ) {
214213
return holder.getEntity();
215214
}
216-
// we need to register a resolution listener only if there is not an already initialized instance
217-
// or an instance that another initializer is loading
218-
registerResolutionListener( data );
219-
return null;
215+
else {
216+
// we need to register a resolution listener only if there is not an already initialized instance
217+
// or an instance that another initializer is loading
218+
registerResolutionListener( data );
219+
return null;
220+
}
220221
}
221222

222223
protected void registerToBatchFetchQueue(Data data) {
@@ -238,7 +239,7 @@ public void initializeInstanceFromParent(Object parentInstance, Data data) {
238239
data.setState( State.MISSING );
239240
}
240241
else {
241-
final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( instance );
242+
final LazyInitializer lazyInitializer = extractLazyInitializer( instance );
242243
if ( lazyInitializer != null && lazyInitializer.isUninitialized() ) {
243244
data.entityKey = new EntityKey( lazyInitializer.getInternalIdentifier(), concreteDescriptor );
244245
registerToBatchFetchQueue( data );
@@ -277,22 +278,13 @@ protected static Object loadInstance(
277278

278279
protected AttributeMapping[] getParentEntityAttributes(String attributeName) {
279280
final EntityPersister entityDescriptor = owningEntityInitializer.getEntityDescriptor();
280-
final AttributeMapping[] parentEntityAttributes = new AttributeMapping[
281-
entityDescriptor.getRootEntityDescriptor()
282-
.getSubclassEntityNames()
283-
.size()
284-
];
285-
parentEntityAttributes[entityDescriptor.getSubclassId()] = getParentEntityAttribute(
286-
entityDescriptor,
287-
toOneMapping,
288-
attributeName
289-
);
281+
final int size = entityDescriptor.getRootEntityDescriptor().getSubclassEntityNames().size();
282+
final AttributeMapping[] parentEntityAttributes = new AttributeMapping[size];
283+
parentEntityAttributes[entityDescriptor.getSubclassId()] =
284+
getParentEntityAttribute( entityDescriptor, toOneMapping, attributeName );
290285
for ( EntityMappingType subMappingType : entityDescriptor.getSubMappingTypes() ) {
291-
parentEntityAttributes[subMappingType.getSubclassId()] = getParentEntityAttribute(
292-
subMappingType,
293-
toOneMapping,
294-
attributeName
295-
);
286+
parentEntityAttributes[subMappingType.getSubclassId()] =
287+
getParentEntityAttribute( subMappingType, toOneMapping, attributeName );
296288
}
297289
return parentEntityAttributes;
298290
}
@@ -302,8 +294,9 @@ protected static AttributeMapping getParentEntityAttribute(
302294
ToOneAttributeMapping referencedModelPart,
303295
String attributeName) {
304296
final AttributeMapping parentAttribute = subMappingType.findAttributeMapping( attributeName );
305-
if ( parentAttribute != null && parentAttribute.getDeclaringType() == referencedModelPart.getDeclaringType()
306-
.findContainingEntityMapping() ) {
297+
if ( parentAttribute != null
298+
&& parentAttribute.getDeclaringType()
299+
== referencedModelPart.getDeclaringType().findContainingEntityMapping() ) {
307300
// These checks are needed to avoid setting the instance using the wrong (child's) model part or
308301
// setting it multiple times in case parent and child share the same attribute name for the association.
309302
return parentAttribute;

hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntitySelectFetchByUniqueKeyInitializer.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
*/
55
package org.hibernate.sql.results.graph.entity.internal;
66

7-
import org.hibernate.EntityFilterException;
8-
import org.hibernate.FetchNotFoundException;
9-
import org.hibernate.annotations.NotFoundAction;
107
import org.hibernate.engine.spi.EntityUniqueKey;
118
import org.hibernate.engine.spi.PersistenceContext;
129
import org.hibernate.engine.spi.SharedSessionContractImplementor;
@@ -37,7 +34,7 @@ public EntitySelectFetchByUniqueKeyInitializer(
3734
}
3835

3936
@Override
40-
protected void initialize(EntitySelectFetchInitializerData data) {
37+
protected void initializeIfNecessary(EntitySelectFetchInitializerData data) {
4138
final String entityName = concreteDescriptor.getEntityName();
4239
final String uniqueKeyPropertyName = fetchedAttribute.getReferencedPropertyName();
4340

@@ -61,18 +58,7 @@ protected void initialize(EntitySelectFetchInitializerData data) {
6158
data.setInstance( instance );
6259

6360
if ( instance == null ) {
64-
if ( toOneMapping.getNotFoundAction() != NotFoundAction.IGNORE ) {
65-
if ( affectedByFilter ) {
66-
throw new EntityFilterException(
67-
entityName,
68-
data.entityIdentifier,
69-
toOneMapping.getNavigableRole().getFullPath()
70-
);
71-
}
72-
if ( toOneMapping.getNotFoundAction() == NotFoundAction.EXCEPTION ) {
73-
throw new FetchNotFoundException( entityName, data.entityIdentifier );
74-
}
75-
}
61+
handleNotFound( data, entityName );
7662
}
7763
// If the entity was not in the Persistence Context, but was found now,
7864
// add it to the Persistence Context

0 commit comments

Comments
 (0)