@@ -131,100 +131,88 @@ protected void resolveInstanceFromIdentifier(Data data) {
131131
132132 @ Override
133133 public void resolveInstance (Object instance , Data data ) {
134- if ( instance == null ) {
134+ final boolean identifierResolved = resolveIdentifier ( instance , data );
135+ if ( data .entityIdentifier == null ) {
135136 data .setState ( State .MISSING );
136137 data .entityKey = null ;
137138 data .setInstance ( null );
138- return ;
139139 }
140- final var rowProcessingState = data .getRowProcessingState ();
141- final var session = rowProcessingState .getSession ();
142- final var persistenceContext = session .getPersistenceContextInternal ();
143- // Only need to extract the identifier if the identifier has a many to one
144- final LazyInitializer lazyInitializer = extractLazyInitializer ( instance );
145- data .entityIdentifier = null ;
146- if ( lazyInitializer == null ) {
147- // Entity is most probably initialized
148- data .setInstance ( instance );
149- final PersistentAttributeInterceptor interceptor ;
150- if ( concreteDescriptor .getBytecodeEnhancementMetadata ().isEnhancedForLazyLoading ()
151- && isPersistentAttributeInterceptable ( instance )
152- && ( interceptor = getAttributeInterceptor ( instance ) ) instanceof EnhancementAsProxyLazinessInterceptor ) {
153- final EnhancementAsProxyLazinessInterceptor enhancementInterceptor = (EnhancementAsProxyLazinessInterceptor ) interceptor ;
154- if ( enhancementInterceptor .isInitialized () ) {
155- data .setState ( State .INITIALIZED );
140+ else {
141+ final var rowProcessingState = data .getRowProcessingState ();
142+ final var session = rowProcessingState .getSession ();
143+ final var persistenceContext = session .getPersistenceContextInternal ();
144+ final LazyInitializer lazyInitializer = extractLazyInitializer ( instance );
145+ if ( lazyInitializer == null ) {
146+ // Entity is most probably initialized
147+ final PersistentAttributeInterceptor interceptor ;
148+ if ( concreteDescriptor .getBytecodeEnhancementMetadata ().isEnhancedForLazyLoading ()
149+ && isPersistentAttributeInterceptable ( instance )
150+ && ( interceptor = getAttributeInterceptor ( instance ) ) instanceof EnhancementAsProxyLazinessInterceptor ) {
151+ final EnhancementAsProxyLazinessInterceptor enhancementInterceptor = (EnhancementAsProxyLazinessInterceptor ) interceptor ;
152+ if ( enhancementInterceptor .isInitialized () ) {
153+ data .setState ( State .INITIALIZED );
154+ }
155+ else {
156+ data .setState ( State .RESOLVED );
157+ }
156158 }
157159 else {
160+ // If the entity initializer is null, we know the entity is fully initialized,
161+ // otherwise it will be initialized by some other initializer
158162 data .setState ( State .RESOLVED );
159- data .entityIdentifier = enhancementInterceptor .getIdentifier ();
160163 }
161164 }
162- else {
163- // If the entity initializer is null, we know the entity is fully initialized,
164- // otherwise it will be initialized by some other initializer
165+ else if ( lazyInitializer .isUninitialized () ) {
165166 data .setState ( State .RESOLVED );
166- data .entityIdentifier = concreteDescriptor .getIdentifier ( instance , session );
167167 }
168- if ( data .entityIdentifier == null ) {
169- data .entityIdentifier = concreteDescriptor .getIdentifier ( instance , session );
168+ else {
169+ // Entity is initialized
170+ data .setState ( State .INITIALIZED );
170171 }
171- }
172- else if ( lazyInitializer .isUninitialized () ) {
173- data .setState ( State .RESOLVED );
174- data .entityIdentifier = lazyInitializer .getInternalIdentifier ();
175- }
176- else {
177- // Entity is initialized
178- data .setState ( State .INITIALIZED );
179- data .entityIdentifier = lazyInitializer .getInternalIdentifier ();
180- data .setInstance ( lazyInitializer .getImplementation () );
181- }
182172
183- data .entityKey = new EntityKey ( data .entityIdentifier , concreteDescriptor );
184- final var entityHolder = persistenceContext .getEntityHolder (
185- data .entityKey
186- );
173+ data .entityKey = new EntityKey ( data .entityIdentifier , concreteDescriptor );
174+ final var entityHolder = persistenceContext .getEntityHolder ( data .entityKey );
187175
188- if ( entityHolder == null || entityHolder . getEntity () != instance && entityHolder . getProxy () != instance ) {
189- // the existing entity instance is detached or transient
190- if ( entityHolder != null ) {
191- final var managed = entityHolder . getManagedObject ();
192- data . setInstance ( managed );
193- data .entityKey = entityHolder . getEntityKey ( );
194- data .entityIdentifier = data . entityKey . getIdentifier ();
195- if ( entityHolder . isInitialized () ) {
196- data .setState ( State .INITIALIZED );
176+ if ( entityHolder == null || instance == null
177+ || entityHolder . getEntity () != instance && entityHolder . getProxy () != instance ) {
178+ // the existing entity instance is detached or transient
179+ if ( entityHolder != null ) {
180+ final var managed = entityHolder . getManagedObject ( );
181+ data .setInstance ( managed );
182+ data .entityKey = entityHolder . getEntityKey ();
183+ data . entityIdentifier = data . entityKey . getIdentifier ();
184+ data .setState ( entityHolder . isInitialized () ? State .INITIALIZED : State . RESOLVED );
197185 }
198186 else {
199187 data .setState ( State .RESOLVED );
200188 }
201189 }
202190 else {
203- data .setState ( State . RESOLVED );
191+ data .setInstance ( instance );
204192 }
205- }
206193
207- if ( data .getState () == State .RESOLVED ) {
208- // similar to resolveInstanceFromIdentifier, but we already have the holder here
209- if ( data .batchDisabled ) {
210- initialize ( data , entityHolder , session , persistenceContext );
211- }
212- else if ( entityHolder == null || !entityHolder .isEventuallyInitialized () ) {
213- // need to add the key to the batch queue only when the entity has not been already loaded or
214- // there isn't another initializer that is loading it
215- registerResolutionListener ( data );
216- registerToBatchFetchQueue ( data );
194+ if ( data .getState () == State .RESOLVED ) {
195+ // similar to resolveInstanceFromIdentifier, but we already have the holder here
196+ if ( data .batchDisabled ) {
197+ initialize ( data , entityHolder , session , persistenceContext );
198+ }
199+ else if ( entityHolder == null || !entityHolder .isEventuallyInitialized () ) {
200+ // need to add the key to the batch queue only when the entity has not been already loaded or
201+ // there isn't another initializer that is loading it
202+ registerResolutionListener ( data );
203+ registerToBatchFetchQueue ( data );
204+ }
217205 }
218- }
219206
220- if ( keyIsEager ) {
221- final Initializer <?> initializer = keyAssembler .getInitializer ();
222- assert initializer != null ;
223- initializer .resolveInstance ( data .entityIdentifier , rowProcessingState );
224- }
225- else if ( rowProcessingState .needsResolveState () ) {
226- // Resolve the state of the identifier if result caching is enabled and this is not a query cache hit
227- keyAssembler .resolveState ( rowProcessingState );
207+ if ( keyIsEager && !identifierResolved ) {
208+ final Initializer <?> initializer = keyAssembler .getInitializer ();
209+ assert initializer != null ;
210+ initializer .resolveInstance ( data .entityIdentifier , rowProcessingState );
211+ }
212+ else if ( rowProcessingState .needsResolveState () && !identifierResolved ) {
213+ // Resolve the state of the identifier if result caching is enabled and this is not a query cache hit
214+ keyAssembler .resolveState ( rowProcessingState );
215+ }
228216 }
229217 }
230218
0 commit comments