86
86
import static org .hibernate .bytecode .enhance .spi .LazyPropertyInitializer .UNFETCHED_PROPERTY ;
87
87
import static org .hibernate .engine .internal .ManagedTypeHelper .asPersistentAttributeInterceptable ;
88
88
import static org .hibernate .engine .internal .ManagedTypeHelper .isPersistentAttributeInterceptable ;
89
- import static org .hibernate .internal .log .LoggingHelper .toLoggableString ;
90
89
import static org .hibernate .metamodel .mapping .ForeignKeyDescriptor .Nature .TARGET ;
91
90
import static org .hibernate .proxy .HibernateProxy .extractLazyInitializer ;
92
91
@@ -163,12 +162,9 @@ public EntityInitializerData(EntityInitializerImpl initializer, RowProcessingSta
163
162
lockMode = rowProcessingState .determineEffectiveLockMode ( initializer .sourceAlias );
164
163
if ( initializer .isResultInitializer () ) {
165
164
uniqueKeyAttributePath = rowProcessingState .getEntityUniqueKeyAttributePath ();
166
- if ( uniqueKeyAttributePath != null ) {
167
- uniqueKeyPropertyTypes = initializer .getParentEntityAttributeTypes ( uniqueKeyAttributePath );
168
- }
169
- else {
170
- uniqueKeyPropertyTypes = null ;
171
- }
165
+ uniqueKeyPropertyTypes = uniqueKeyAttributePath != null
166
+ ? initializer .getParentEntityAttributeTypes ( uniqueKeyAttributePath )
167
+ : null ;
172
168
canUseEmbeddedIdentifierInstanceAsEntity = rowProcessingState .getEntityId () != null
173
169
&& initializer .couldUseEmbeddedIdentifierInstanceAsEntity ;
174
170
}
@@ -178,13 +174,10 @@ public EntityInitializerData(EntityInitializerImpl initializer, RowProcessingSta
178
174
canUseEmbeddedIdentifierInstanceAsEntity = false ;
179
175
}
180
176
hasCallbackActions = rowProcessingState .hasCallbackActions ();
181
- if ( initializer .discriminatorAssembler == null
182
- || rowProcessingState .isQueryCacheHit () && entityDescriptor .useShallowQueryCacheLayout () && !entityDescriptor .storeDiscriminatorInShallowQueryCacheLayout () ) {
183
- defaultConcreteDescriptor = entityDescriptor ;
184
- }
185
- else {
186
- defaultConcreteDescriptor = null ;
187
- }
177
+ defaultConcreteDescriptor =
178
+ hasConcreteDescriptor ( rowProcessingState , initializer .discriminatorAssembler , entityDescriptor )
179
+ ? entityDescriptor
180
+ : null ;
188
181
}
189
182
190
183
/*
@@ -206,6 +199,16 @@ public EntityInitializerData(EntityInitializerData original) {
206
199
}
207
200
}
208
201
202
+ private static boolean hasConcreteDescriptor (
203
+ RowProcessingState rowProcessingState ,
204
+ BasicResultAssembler <?> discriminatorAssembler ,
205
+ EntityPersister entityDescriptor ) {
206
+ return discriminatorAssembler == null
207
+ || rowProcessingState .isQueryCacheHit ()
208
+ && entityDescriptor .useShallowQueryCacheLayout ()
209
+ && !entityDescriptor .storeDiscriminatorInShallowQueryCacheLayout ();
210
+ }
211
+
209
212
public EntityInitializerImpl (
210
213
EntityResultGraphNode resultDescriptor ,
211
214
String sourceAlias ,
@@ -229,8 +232,9 @@ public EntityInitializerImpl(
229
232
: entityDescriptor .getRootEntityDescriptor ().getEntityPersister ();
230
233
keyTypeForEqualsHashCode = entityDescriptor .getIdentifierType ().getTypeForEqualsHashCode ();
231
234
// The id can only be the entity instance if this is a non-aggregated id that has no containing class
232
- couldUseEmbeddedIdentifierInstanceAsEntity = entityDescriptor .getIdentifierMapping () instanceof CompositeIdentifierMapping
233
- && !( (CompositeIdentifierMapping ) entityDescriptor .getIdentifierMapping () ).hasContainingClass ();
235
+ couldUseEmbeddedIdentifierInstanceAsEntity =
236
+ entityDescriptor .getIdentifierMapping () instanceof CompositeIdentifierMapping
237
+ && !( (CompositeIdentifierMapping ) entityDescriptor .getIdentifierMapping () ).hasContainingClass ();
234
238
235
239
this .navigablePath = resultDescriptor .getNavigablePath ();
236
240
this .sourceAlias = sourceAlias ;
@@ -258,7 +262,8 @@ public EntityInitializerImpl(
258
262
hasKeyManyToOne = initializer != null && initializer .isLazyCapable ();
259
263
}
260
264
261
- assert entityDescriptor .hasSubclasses () == (discriminatorFetch != null ) : "Discriminator should only be fetched if the entity has subclasses" ;
265
+ assert entityDescriptor .hasSubclasses () == (discriminatorFetch != null )
266
+ : "Discriminator should only be fetched if the entity has subclasses" ;
262
267
discriminatorAssembler = discriminatorFetch != null
263
268
? (BasicResultAssembler <?>) discriminatorFetch .createAssembler ( this , creationState )
264
269
: null ;
@@ -286,12 +291,16 @@ public EntityInitializerImpl(
286
291
final BitSet [] lazySets = new BitSet [subMappingTypes .size () + 1 ];
287
292
final BitSet [] maybeLazySets = new BitSet [subMappingTypes .size () + 1 ];
288
293
final MutabilityPlan [][] updatableAttributeMutabilityPlans = new MutabilityPlan [subMappingTypes .size () + 1 ][];
289
- assemblers [rootEntityDescriptor .getSubclassId ()] = new DomainResultAssembler [rootEntityDescriptor .getNumberOfFetchables ()];
290
- updatableAttributeMutabilityPlans [rootEntityDescriptor .getSubclassId ()] = new MutabilityPlan [rootEntityDescriptor .getNumberOfAttributeMappings ()];
294
+ assemblers [rootEntityDescriptor .getSubclassId ()] =
295
+ new DomainResultAssembler [rootEntityDescriptor .getNumberOfFetchables ()];
296
+ updatableAttributeMutabilityPlans [rootEntityDescriptor .getSubclassId ()] =
297
+ new MutabilityPlan [rootEntityDescriptor .getNumberOfAttributeMappings ()];
291
298
292
299
for ( EntityMappingType subMappingType : subMappingTypes ) {
293
- assemblers [subMappingType .getSubclassId ()] = new DomainResultAssembler [subMappingType .getNumberOfFetchables ()];
294
- updatableAttributeMutabilityPlans [subMappingType .getSubclassId ()] = new MutabilityPlan [subMappingType .getNumberOfAttributeMappings ()];
300
+ assemblers [subMappingType .getSubclassId ()] =
301
+ new DomainResultAssembler [subMappingType .getNumberOfFetchables ()];
302
+ updatableAttributeMutabilityPlans [subMappingType .getSubclassId ()] =
303
+ new MutabilityPlan [subMappingType .getNumberOfAttributeMappings ()];
295
304
}
296
305
297
306
boolean hasLazySubInitializers = false ;
@@ -345,7 +354,8 @@ public EntityInitializerImpl(
345
354
}
346
355
for ( EntityMappingType subMappingType : declaringType .getSubMappingTypes () ) {
347
356
assemblers [subMappingType .getSubclassId ()][stateArrayPosition ] = stateAssembler ;
348
- updatableAttributeMutabilityPlans [subMappingType .getSubclassId ()][stateArrayPosition ] = updatableAttributeMutabilityPlans [subclassId ][stateArrayPosition ];
357
+ updatableAttributeMutabilityPlans [subMappingType .getSubclassId ()][stateArrayPosition ] =
358
+ updatableAttributeMutabilityPlans [subclassId ][stateArrayPosition ];
349
359
if ( subInitializer != null ) {
350
360
if ( subInitializers [subMappingType .getSubclassId ()] == null ) {
351
361
subInitializers [subMappingType .getSubclassId ()] = new Initializer <?>[size ];
@@ -355,12 +365,14 @@ public EntityInitializerImpl(
355
365
maybeLazySets [subMappingType .getSubclassId ()] = new BitSet (size );
356
366
}
357
367
subInitializers [subMappingType .getSubclassId ()][stateArrayPosition ] = subInitializer ;
358
- eagerSubInitializers [subMappingType .getSubclassId ()][stateArrayPosition ] = eagerSubInitializers [subclassId ][stateArrayPosition ];
359
- collectionContainingSubInitializers [subMappingType .getSubclassId ()][stateArrayPosition ] = collectionContainingSubInitializers [subclassId ][stateArrayPosition ];
360
- if (lazySets [subclassId ].get ( stateArrayPosition ) ) {
368
+ eagerSubInitializers [subMappingType .getSubclassId ()][stateArrayPosition ] =
369
+ eagerSubInitializers [subclassId ][stateArrayPosition ];
370
+ collectionContainingSubInitializers [subMappingType .getSubclassId ()][stateArrayPosition ] =
371
+ collectionContainingSubInitializers [subclassId ][stateArrayPosition ];
372
+ if ( lazySets [subclassId ].get ( stateArrayPosition ) ) {
361
373
lazySets [subMappingType .getSubclassId ()].set ( stateArrayPosition );
362
374
}
363
- if (maybeLazySets [subclassId ].get ( stateArrayPosition ) ) {
375
+ if ( maybeLazySets [subclassId ].get ( stateArrayPosition ) ) {
364
376
maybeLazySets [subMappingType .getSubclassId ()].set ( stateArrayPosition );
365
377
}
366
378
}
@@ -411,9 +423,10 @@ public EntityInitializerImpl(
411
423
412
424
this .assemblers = assemblers ;
413
425
this .subInitializers = subInitializers ;
414
- this .subInitializersForResolveFromInitialized = rootEntityDescriptor .getBytecodeEnhancementMetadata ().isEnhancedForLazyLoading ()
415
- ? subInitializers
416
- : eagerSubInitializers ;
426
+ this .subInitializersForResolveFromInitialized =
427
+ rootEntityDescriptor .getBytecodeEnhancementMetadata ().isEnhancedForLazyLoading ()
428
+ ? subInitializers
429
+ : eagerSubInitializers ;
417
430
this .collectionContainingSubInitializers = collectionContainingSubInitializers ;
418
431
this .lazySets = Arrays .stream ( lazySets ).map ( ImmutableBitSet ::valueOf ).toArray ( ImmutableBitSet []::new );
419
432
this .maybeLazySets = Arrays .stream ( maybeLazySets )
@@ -531,7 +544,8 @@ protected void resolveKey(EntityInitializerData data, boolean entityKeyOnly) {
531
544
}
532
545
else {
533
546
//noinspection unchecked
534
- final Initializer <InitializerData > initializer = (Initializer <InitializerData >) identifierAssembler .getInitializer ();
547
+ final Initializer <InitializerData > initializer =
548
+ (Initializer <InitializerData >) identifierAssembler .getInitializer ();
535
549
if ( initializer != null ) {
536
550
final InitializerData subData = initializer .getData ( rowProcessingState );
537
551
initializer .resolveKey ( subData );
@@ -785,7 +799,8 @@ public boolean isResultInitializer() {
785
799
}
786
800
787
801
private void deepCopy (EntityPersister containerDescriptor , Object [] source , Object [] target ) {
788
- final MutabilityPlan <Object >[] updatableAttributeMutabilityPlan = updatableAttributeMutabilityPlans [containerDescriptor .getSubclassId ()];
802
+ final MutabilityPlan <Object >[] updatableAttributeMutabilityPlan =
803
+ updatableAttributeMutabilityPlans [containerDescriptor .getSubclassId ()];
789
804
for ( int i = 0 ; i < updatableAttributeMutabilityPlan .length ; i ++ ) {
790
805
final Object sourceValue = source [i ];
791
806
if ( updatableAttributeMutabilityPlan [i ] != null
@@ -830,11 +845,7 @@ public Object getTargetInstance(EntityInitializerData data) {
830
845
protected Type [] getParentEntityAttributeTypes (String attributeName ) {
831
846
Type [] types = parentEntityAttributeTypes .get ( attributeName );
832
847
if ( types == null ) {
833
- types = new Type [
834
- entityDescriptor .getRootEntityDescriptor ()
835
- .getSubclassEntityNames ()
836
- .size ()
837
- ];
848
+ types = new Type [entityDescriptor .getRootEntityDescriptor ().getSubclassEntityNames ().size ()];
838
849
initializeAttributeType ( types , entityDescriptor , attributeName );
839
850
for ( EntityMappingType subMappingType : entityDescriptor .getSubMappingTypes () ) {
840
851
initializeAttributeType ( types , subMappingType .getEntityPersister (), attributeName );
@@ -855,12 +866,12 @@ protected void initializeAttributeType(Type[] attributeTypes, EntityPersister en
855
866
@ Nullable BasicResultAssembler <?> discriminatorAssembler ,
856
867
EntityPersister entityDescriptor )
857
868
throws WrongClassException {
858
- if ( discriminatorAssembler == null
859
- || rowProcessingState .isQueryCacheHit () && entityDescriptor .useShallowQueryCacheLayout () && !entityDescriptor .storeDiscriminatorInShallowQueryCacheLayout () ) {
869
+ if ( hasConcreteDescriptor ( rowProcessingState , discriminatorAssembler , entityDescriptor ) ) {
860
870
return entityDescriptor ;
861
871
}
862
872
else {
863
- assert entityDescriptor .hasSubclasses () : "Reading a discriminator from a result set should only happen if the entity has subclasses" ;
873
+ assert entityDescriptor .hasSubclasses ()
874
+ : "Reading a discriminator from a result set should only happen if the entity has subclasses" ;
864
875
final EntityDiscriminatorMapping discriminatorMapping = entityDescriptor .getDiscriminatorMapping ();
865
876
assert discriminatorMapping != null ;
866
877
final Object discriminator = discriminatorAssembler .extractRawValue ( rowProcessingState );
@@ -888,9 +899,16 @@ protected void initializeAttributeType(Type[] attributeTypes, EntityPersister en
888
899
}
889
900
890
901
protected boolean useEmbeddedIdentifierInstanceAsEntity (EntityInitializerData data ) {
891
- return data .canUseEmbeddedIdentifierInstanceAsEntity
892
- && ( data .concreteDescriptor = determineConcreteEntityDescriptor ( data .getRowProcessingState (), discriminatorAssembler , entityDescriptor ) ) != null
902
+ if ( data .canUseEmbeddedIdentifierInstanceAsEntity ) {
903
+ data .concreteDescriptor =
904
+ determineConcreteEntityDescriptor ( data .getRowProcessingState (),
905
+ discriminatorAssembler , entityDescriptor );
906
+ return data .concreteDescriptor != null
893
907
&& data .concreteDescriptor .isInstance ( data .getRowProcessingState ().getEntityId () );
908
+ }
909
+ else {
910
+ return false ;
911
+ }
894
912
}
895
913
896
914
@ Override
@@ -918,7 +936,7 @@ public void resolveInstance(Object instance, EntityInitializerData data) {
918
936
resolveKey ( data );
919
937
assert data .getState () == State .MISSING ;
920
938
assert referencedModelPart instanceof ToOneAttributeMapping
921
- && ( (ToOneAttributeMapping ) referencedModelPart ).getSideNature () == TARGET ;
939
+ && ( (ToOneAttributeMapping ) referencedModelPart ).getSideNature () == TARGET ;
922
940
return ;
923
941
}
924
942
// If the entity initializer is null, we know the entity is fully initialized,
@@ -1132,7 +1150,7 @@ protected void upgradeLockMode(EntityInitializerData data) {
1132
1150
1133
1151
private boolean isProxyInstance (Object proxy ) {
1134
1152
return proxy != null
1135
- && ( proxy instanceof MapProxy || entityDescriptor .getJavaType ().getJavaTypeClass ().isInstance ( proxy ) );
1153
+ && ( proxy instanceof MapProxy || entityDescriptor .getJavaType ().getJavaTypeClass ().isInstance ( proxy ) );
1136
1154
}
1137
1155
1138
1156
private boolean isExistingEntityInitialized (Object existingEntity ) {
0 commit comments