1414import org .hibernate .metamodel .mapping .EmbeddableMappingType ;
1515import org .hibernate .metamodel .mapping .EmbeddableValuedModelPart ;
1616import org .hibernate .metamodel .mapping .VirtualModelPart ;
17- import org .hibernate .metamodel .spi .EmbeddableInstantiator ;
1817import org .hibernate .metamodel .spi .ValueAccess ;
1918import org .hibernate .property .access .spi .PropertyAccess ;
2019import org .hibernate .property .access .spi .Setter ;
21- import org .hibernate .proxy .HibernateProxy ;
22- import org .hibernate .proxy .LazyInitializer ;
2320import org .hibernate .spi .NavigablePath ;
2421import org .hibernate .sql .results .graph .AssemblerCreationState ;
2522import org .hibernate .sql .results .graph .DomainResult ;
3229import org .hibernate .sql .results .graph .InitializerParent ;
3330import org .hibernate .sql .results .graph .basic .BasicFetch ;
3431import org .hibernate .sql .results .graph .basic .BasicResultAssembler ;
35- import org .hibernate .sql .results .graph .collection .CollectionInitializer ;
3632import org .hibernate .sql .results .graph .embeddable .EmbeddableInitializer ;
3733import org .hibernate .sql .results .graph .embeddable .EmbeddableResultGraphNode ;
3834import org .hibernate .sql .results .graph .entity .EntityInitializer ;
4238
4339import org .checkerframework .checker .nullness .qual .Nullable ;
4440
41+ import static org .hibernate .proxy .HibernateProxy .extractLazyInitializer ;
4542import static org .hibernate .sql .results .graph .entity .internal .BatchEntityInsideEmbeddableSelectFetchInitializer .BATCH_PROPERTY ;
4643
4744/**
@@ -361,9 +358,9 @@ public void resolveInstance(@Nullable Object instance, EmbeddableInitializerData
361358 }
362359
363360 private void resolveInstanceSubInitializers (int subclassId , Object instance , RowProcessingState rowProcessingState ) {
364- final Initializer <?>[] initializers = subInitializersForResolveFromInitialized [subclassId ];
361+ final var initializers = subInitializersForResolveFromInitialized [subclassId ];
365362 for ( int i = 0 ; i < initializers .length ; i ++ ) {
366- final Initializer <?> initializer = initializers [i ];
363+ final var initializer = initializers [i ];
367364 if ( initializer != null ) {
368365 final Object subInstance = embeddableMappingType .getValue ( instance , i );
369366 if ( subInstance == LazyPropertyInitializer .UNFETCHED_PROPERTY ) {
@@ -387,7 +384,7 @@ public void initializeInstance(EmbeddableInitializerData data) {
387384 if ( embedded .getParentInjectionAttributePropertyAccess () != null || embedded instanceof VirtualModelPart ) {
388385 handleParentInjection ( data );
389386
390- final LazyInitializer lazyInitializer = HibernateProxy . extractLazyInitializer ( data .getInstance () );
387+ final var lazyInitializer = extractLazyInitializer ( data .getInstance () );
391388 // If the composite instance has a lazy initializer attached, this means that the embeddable is actually virtual
392389 // and the compositeInstance == entity, so we have to inject the row state into the entity when it finishes resolution
393390 if ( lazyInitializer != null ) {
@@ -415,19 +412,19 @@ public void initializeInstance(EmbeddableInitializerData data) {
415412
416413 @ Override
417414 protected void forEachSubInitializer (BiConsumer <Initializer <?>, RowProcessingState > consumer , InitializerData data ) {
418- final EmbeddableInitializerData embeddableInitializerData = (EmbeddableInitializerData ) data ;
419- final RowProcessingState rowProcessingState = embeddableInitializerData .getRowProcessingState ();
415+ final var embeddableInitializerData = (EmbeddableInitializerData ) data ;
416+ final var rowProcessingState = embeddableInitializerData .getRowProcessingState ();
420417 if ( embeddableInitializerData .concreteEmbeddableType == null ) {
421- for ( Initializer <?>[] initializers : subInitializers ) {
422- for ( Initializer <?> initializer : initializers ) {
418+ for ( var initializers : subInitializers ) {
419+ for ( var initializer : initializers ) {
423420 if ( initializer != null ) {
424421 consumer .accept ( initializer , rowProcessingState );
425422 }
426423 }
427424 }
428425 }
429426 else {
430- for ( Initializer <?> initializer : subInitializers [embeddableInitializerData .getSubclassId ()] ) {
427+ for ( var initializer : subInitializers [embeddableInitializerData .getSubclassId ()] ) {
431428 if ( initializer != null ) {
432429 consumer .accept ( initializer , rowProcessingState );
433430 }
@@ -460,12 +457,18 @@ private void prepareCompositeInstance(EmbeddableInitializerData data) {
460457 // Virtual model parts use the owning entity as container which the fetch parent access provides.
461458 // For an identifier or foreign key this is called during the resolveKey phase of the fetch parent,
462459 // so we can't use the fetch parent access in that case.
463- if ( parent != null && embedded instanceof VirtualModelPart && !isPartOfKey && data .getState () != State .MISSING ) {
464- final InitializerData subData = parent .getData ( data .getRowProcessingState () );
465- parent .resolveInstance ( subData );
466- data .setInstance ( ((EntityInitializer <InitializerData >) parent ).getTargetInstance ( subData ) );
467- if ( data .getState () == State .INITIALIZED ) {
468- return ;
460+ if ( parent != null && embedded instanceof VirtualModelPart && !isPartOfKey ) {
461+ final State state = data .getState ();
462+ if ( state != State .MISSING ) {
463+ final InitializerData subData = parent .getData ( data .getRowProcessingState () );
464+ parent .resolveInstance ( subData );
465+ final Object targetInstance =
466+ ((EntityInitializer <InitializerData >) parent )
467+ .getTargetInstance ( subData );
468+ data .setInstance ( targetInstance );
469+ if ( state == State .INITIALIZED ) {
470+ return ;
471+ }
469472 }
470473 }
471474
@@ -478,19 +481,13 @@ private void prepareCompositeInstance(EmbeddableInitializerData data) {
478481
479482 protected void extractRowState (EmbeddableInitializerData data ) {
480483 boolean stateAllNull = true ;
481- final DomainResultAssembler <?>[] subAssemblers = assemblers [data .getSubclassId ()];
482- final RowProcessingState rowProcessingState = data .getRowProcessingState ();
484+ final var subAssemblers = assemblers [data .getSubclassId ()];
485+ final var rowProcessingState = data .getRowProcessingState ();
483486 final Object [] rowState = data .rowState ;
484487 for ( int i = 0 ; i < subAssemblers .length ; i ++ ) {
485- final DomainResultAssembler <?> assembler = subAssemblers [i ];
488+ final var assembler = subAssemblers [i ];
486489 final Object contributorValue = assembler == null ? null : assembler .assemble ( rowProcessingState );
487-
488- if ( contributorValue == BATCH_PROPERTY ) {
489- rowState [i ] = null ;
490- }
491- else {
492- rowState [i ] = contributorValue ;
493- }
490+ rowState [i ] = contributorValue == BATCH_PROPERTY ? null : contributorValue ;
494491 if ( contributorValue != null ) {
495492 stateAllNull = false ;
496493 }
@@ -512,8 +509,8 @@ protected boolean isNull(EmbeddableInitializerData data) {
512509
513510 @ Override
514511 public void resolveState (EmbeddableInitializerData data ) {
515- final RowProcessingState rowProcessingState = data .getRowProcessingState ();
516- for ( final DomainResultAssembler <?> assembler : assemblers [data .getSubclassId ()] ) {
512+ final var rowProcessingState = data .getRowProcessingState ();
513+ for ( var assembler : assemblers [data .getSubclassId ()] ) {
517514 assembler .resolveState ( rowProcessingState );
518515 }
519516 }
@@ -522,33 +519,30 @@ private Object createCompositeInstance(EmbeddableInitializerData data) {
522519 if ( data .getState () == State .MISSING ) {
523520 return null ;
524521 }
525-
526- final EmbeddableInstantiator instantiator = data .concreteEmbeddableType == null
527- ? embeddableMappingType .getRepresentationStrategy ().getInstantiator ()
528- : data .concreteEmbeddableType .getInstantiator ();
529- final Object instance = instantiator .instantiate ( data );
530- data .setState ( State .RESOLVED );
531- // EMBEDDED_LOAD_LOGGER.tracef( "Created composite instance [%s]: %s", navigablePath, instance );
532- return instance ;
522+ else {
523+ final var instantiator =
524+ data .concreteEmbeddableType == null
525+ ? embeddableMappingType .getRepresentationStrategy ().getInstantiator ()
526+ : data .concreteEmbeddableType .getInstantiator ();
527+ final Object instance = instantiator .instantiate ( data );
528+ data .setState ( State .RESOLVED );
529+ // EMBEDDED_LOAD_LOGGER.tracef( "Created composite instance [%s]: %s", navigablePath, instance );
530+ return instance ;
531+ }
533532 }
534533
535534 private void handleParentInjection (EmbeddableInitializerData data ) {
536535 final PropertyAccess parentInjectionAccess = embedded .getParentInjectionAttributePropertyAccess ();
537- if ( parentInjectionAccess == null ) {
538- // embeddable defined no parent injection
539- return ;
540- }
541-
542- final Initializer <?> owningInitializer = determineOwningInitializer ();
543- final Object parent = determineParentInstance ( owningInitializer , data .getRowProcessingState () );
544- if ( parent == null ) {
545- return ;
536+ if ( parentInjectionAccess != null ) {
537+ final Object parent =
538+ determineParentInstance ( determineOwningInitializer (), data .getRowProcessingState () );
539+ if ( parent != null ) {
540+ final Setter setter = parentInjectionAccess .getSetter ();
541+ assert setter != null ;
542+ setter .set ( data .getInstance (), parent );
543+ }
546544 }
547-
548- final Setter setter = parentInjectionAccess .getSetter ();
549- assert setter != null ;
550-
551- setter .set ( data .getInstance (), parent );
545+ // else embeddable defined no parent injection
552546 }
553547
554548 private Initializer <?> determineOwningInitializer () {
@@ -570,12 +564,12 @@ private Object determineParentInstance(Initializer<?> parentInitializer, RowProc
570564 throw new UnsupportedOperationException ( "Cannot determine Embeddable: " + navigablePath + " parent instance, parent initializer is null" );
571565 }
572566
573- final CollectionInitializer <?> collectionInitializer = parentInitializer .asCollectionInitializer ();
567+ final var collectionInitializer = parentInitializer .asCollectionInitializer ();
574568 if ( collectionInitializer != null ) {
575569 return collectionInitializer .getCollectionInstance ( rowProcessingState ).getOwner ();
576570 }
577571
578- final EntityInitializer <?> parentEntityInitializer = parentInitializer .asEntityInitializer ();
572+ final var parentEntityInitializer = parentInitializer .asEntityInitializer ();
579573 if ( parentEntityInitializer != null ) {
580574 return parentEntityInitializer .getTargetInstance ( rowProcessingState );
581575 }
0 commit comments