5
5
package org .hibernate .metamodel .internal ;
6
6
7
7
import jakarta .persistence .metamodel .Attribute ;
8
- import jakarta .persistence .metamodel .SingularAttribute ;
9
8
import jakarta .persistence .metamodel .Type ;
10
9
import org .hibernate .AssertionFailure ;
11
10
import org .hibernate .Internal ;
@@ -265,12 +264,12 @@ public Map<String, IdentifiableDomainType<?>> getIdentifiableTypesByName() {
265
264
final Property genericProperty = property .copy ();
266
265
genericProperty .setValue ( genericComponent );
267
266
genericProperty .setGeneric ( true );
268
- final PersistentAttribute < X , ?> attribute = factoryFunction .apply ( entityType , genericProperty );
267
+ final var attribute = factoryFunction .apply ( entityType , genericProperty );
269
268
if ( !property .isGeneric () ) {
270
- final PersistentAttribute < X , ?> concreteAttribute = factoryFunction .apply ( entityType , property );
269
+ final var concreteAttribute = factoryFunction .apply ( entityType , property );
271
270
if ( concreteAttribute != null ) {
272
- @ SuppressWarnings ( "unchecked" )
273
- final AttributeContainer < X > attributeContainer = (AttributeContainer <X >) entityType ;
271
+ final var managedType = ( ManagedDomainType < X >) entityType ;
272
+ final var attributeContainer = (AttributeContainer <X >) managedType ;
274
273
attributeContainer .getInFlightAccess ().addConcreteGenericAttribute ( concreteAttribute );
275
274
}
276
275
}
@@ -383,7 +382,7 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
383
382
384
383
embeddablesToProcess .clear ();
385
384
386
- for ( EmbeddableDomainType <?> embeddable : processingEmbeddables ) {
385
+ for ( var embeddable : processingEmbeddables ) {
387
386
final Component component = componentByEmbeddable .get ( embeddable );
388
387
for ( Property property : component .getProperties () ) {
389
388
if ( !component .isPolymorphic ()
@@ -395,7 +394,8 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
395
394
( ( AttributeContainer <?>) embeddable ).getInFlightAccess ().finishUp ();
396
395
// Do not process embeddables for entity types i.e. id-classes or
397
396
// generic component embeddables used just for concrete type resolution
398
- if ( !component .isGeneric () && !( embeddable .getExpressibleJavaType () instanceof EntityJavaType <?> ) ) {
397
+ if ( !component .isGeneric ()
398
+ && !( embeddable .getExpressibleJavaType () instanceof EntityJavaType <?> ) ) {
399
399
embeddables .put ( embeddable .getJavaType (), embeddable );
400
400
if ( staticMetamodelScanEnabled ) {
401
401
populateStaticMetamodel ( embeddable , processedMetamodelClasses );
@@ -419,8 +419,8 @@ private <T> void addAttribute(EmbeddableDomainType<T> embeddable, Property prope
419
419
getMappedSuperclassProperty ( property .getName (),
420
420
component .getMappedSuperclass () );
421
421
if ( superclassProperty != null && superclassProperty .isGeneric () ) {
422
- @ SuppressWarnings ( "unchecked" )
423
- final var attributeContainer = (AttributeContainer <T >) embeddable ;
422
+ final var managedType = ( ManagedDomainType < T >) embeddable ;
423
+ final var attributeContainer = (AttributeContainer <T >) managedType ;
424
424
attributeContainer .getInFlightAccess ().addConcreteGenericAttribute ( attribute );
425
425
}
426
426
else {
@@ -430,29 +430,26 @@ private <T> void addAttribute(EmbeddableDomainType<T> embeddable, Property prope
430
430
}
431
431
432
432
private <T > void buildAttribute (Property property , IdentifiableDomainType <T > jpaType ) {
433
- final PersistentAttribute <T , ?> attribute =
434
- buildAttribute ( property , jpaType , attributeFactory ::buildAttribute );
433
+ final var attribute = buildAttribute ( property , jpaType , attributeFactory ::buildAttribute );
435
434
if ( attribute != null ) {
436
435
addAttribute ( jpaType , attribute );
437
436
if ( property .isNaturalIdentifier () ) {
438
- @ SuppressWarnings ( "unchecked" )
439
- final AttributeContainer < T > attributeContainer = (AttributeContainer <T >) jpaType ;
437
+ final var managedType = ( ManagedDomainType < T >) jpaType ;
438
+ final var attributeContainer = (AttributeContainer <T >) managedType ;
440
439
attributeContainer .getInFlightAccess ().applyNaturalIdAttribute ( attribute );
441
440
}
442
441
}
443
442
}
444
443
445
444
private <T > void addAttribute (ManagedDomainType <T > type , PersistentAttribute <T , ?> attribute ) {
446
- @ SuppressWarnings ("unchecked" )
447
- final AttributeContainer <T > container = (AttributeContainer <T >) type ;
448
- final AttributeContainer .InFlightAccess <T > inFlightAccess = container .getInFlightAccess ();
445
+ final var container = (AttributeContainer <T >) type ;
446
+ final var inFlightAccess = container .getInFlightAccess ();
449
447
final boolean virtual =
450
448
attribute .getPersistentAttributeType () == Attribute .PersistentAttributeType .EMBEDDED
451
449
&& attribute .getAttributeJavaType () instanceof EntityJavaType <?>;
452
450
if ( virtual ) {
453
451
@ SuppressWarnings ("unchecked" )
454
- final EmbeddableDomainType <T > embeddableDomainType =
455
- (EmbeddableDomainType <T >) attribute .getValueGraphType ();
452
+ final var embeddableDomainType = (EmbeddableDomainType <T >) attribute .getValueGraphType ();
456
453
final Component component = componentByEmbeddable .get ( embeddableDomainType );
457
454
for ( Property property : component .getProperties () ) {
458
455
final PersistentAttribute <T , ?> subAttribute =
@@ -474,9 +471,9 @@ private <T> void addAttribute(ManagedDomainType<T> type, PersistentAttribute<T,
474
471
475
472
private <T > void applyIdMetadata (PersistentClass persistentClass , IdentifiableDomainType <T > identifiableType ) {
476
473
if ( persistentClass .hasIdentifierProperty () ) {
474
+ final var managedType = (ManagedDomainType <T >) identifiableType ;
475
+ final var attributeContainer = (AttributeContainer <T >) managedType ;
477
476
final Property declaredIdentifierProperty = persistentClass .getDeclaredIdentifierProperty ();
478
- @ SuppressWarnings ("unchecked" )
479
- final AttributeContainer <T > attributeContainer = (AttributeContainer <T >) identifiableType ;
480
477
if ( declaredIdentifierProperty != null ) {
481
478
final var idAttribute =
482
479
(SingularPersistentAttribute <T , ?>)
@@ -509,13 +506,10 @@ private <T> void applyIdMetadata(PersistentClass persistentClass, IdentifiableDo
509
506
if ( identifierMapper != null ) {
510
507
cidProperties = identifierMapper .getProperties ();
511
508
propertySpan = identifierMapper .getPropertySpan ();
512
- if ( identifierMapper .getComponentClassName () == null ) {
513
- // support for no id-class, especially for dynamic models
514
- idClassType = null ;
515
- }
516
- else {
517
- idClassType = applyIdClassMetadata ( (Component ) persistentClass .getIdentifier () );
518
- }
509
+ idClassType =
510
+ identifierMapper .getComponentClassName () == null
511
+ ? null // support for no id-class, especially for dynamic models
512
+ : applyIdClassMetadata ( (Component ) persistentClass .getIdentifier () );
519
513
}
520
514
else {
521
515
cidProperties = compositeId .getProperties ();
@@ -525,8 +519,7 @@ private <T> void applyIdMetadata(PersistentClass persistentClass, IdentifiableDo
525
519
526
520
assert compositeId .isEmbedded ();
527
521
528
- final IdentifiableDomainType <?> idDomainType =
529
- identifiableTypesByName .get ( compositeId .getOwner ().getEntityName () );
522
+ final var idDomainType = identifiableTypesByName .get ( compositeId .getOwner ().getEntityName () );
530
523
@ SuppressWarnings ("unchecked" )
531
524
final var idType = (AbstractIdentifiableType <T >) idDomainType ;
532
525
applyIdAttributes ( identifiableType , idType , propertySpan , cidProperties , idClassType );
@@ -539,17 +532,29 @@ private <T> void applyIdAttributes(
539
532
int propertySpan ,
540
533
List <Property > cidProperties ,
541
534
EmbeddableTypeImpl <?> idClassType ) {
542
- Set <SingularPersistentAttribute <? super T , ?>> idAttributes = idType .getIdClassAttributesSafely ();
543
- if ( idAttributes == null ) {
544
- idAttributes = new HashSet <>( propertySpan );
535
+ final var idAttributes = idClassAttributes ( idType , propertySpan , cidProperties );
536
+ final var managedType = (ManagedDomainType <T >) identifiableType ;
537
+ final var container = (AttributeContainer <T >) managedType ;
538
+ container .getInFlightAccess ().applyNonAggregatedIdAttributes ( idAttributes , idClassType );
539
+ }
540
+
541
+ private <T > Set <SingularPersistentAttribute <? super T , ?>> idClassAttributes (
542
+ AbstractIdentifiableType <T > idType ,
543
+ int propertySpan ,
544
+ List <Property > cidProperties ) {
545
+ final var idAttributes = idType .getIdClassAttributesSafely ();
546
+ if ( idAttributes != null ) {
547
+ return idAttributes ;
548
+ }
549
+ else {
550
+ final Set <SingularPersistentAttribute <? super T , ?>> result = new HashSet <>( propertySpan );
545
551
for ( Property cidSubproperty : cidProperties ) {
546
- idAttributes .add ( attributeFactory .buildIdAttribute ( idType , cidSubproperty ) );
552
+ final SingularPersistentAttribute <T , ?> idAttribute =
553
+ attributeFactory .buildIdAttribute ( idType , cidSubproperty );
554
+ result .add ( idAttribute );
547
555
}
556
+ return result ;
548
557
}
549
-
550
- @ SuppressWarnings ("unchecked" )
551
- final var container = (AttributeContainer <T >) identifiableType ;
552
- container .getInFlightAccess ().applyNonAggregatedIdAttributes ( idAttributes , idClassType );
553
558
}
554
559
555
560
private Property getMappedSuperclassIdentifier (PersistentClass persistentClass ) {
@@ -565,10 +570,9 @@ private Property getMappedSuperclassIdentifier(PersistentClass persistentClass)
565
570
}
566
571
567
572
private <Y > EmbeddableTypeImpl <Y > applyIdClassMetadata (Component idClassComponent ) {
568
- final EmbeddableTypeImpl <Y > embeddableType =
569
- new EmbeddableTypeImpl <>(
570
- getTypeConfiguration ().getJavaTypeRegistry ()
571
- .resolveManagedTypeDescriptor ( idClassComponent .getComponentClass () ),
573
+ final var embeddableType =
574
+ new EmbeddableTypeImpl <Y >(
575
+ getJavaTypeRegistry ().resolveManagedTypeDescriptor ( idClassComponent .getComponentClass () ),
572
576
getMappedSuperclassDomainType ( idClassComponent ),
573
577
null ,
574
578
false ,
@@ -587,8 +591,8 @@ private <Y> MappedSuperclassDomainType<? super Y> getMappedSuperclassDomainType(
587
591
}
588
592
589
593
private <X > void applyIdMetadata (MappedSuperclass mappingType , MappedSuperclassDomainType <X > jpaMappingType ) {
590
- @ SuppressWarnings ( "unchecked" )
591
- final var attributeContainer = (AttributeContainer <X >) jpaMappingType ;
594
+ final var managedType = ( ManagedDomainType < X >) jpaMappingType ;
595
+ final var attributeContainer = (AttributeContainer <X >) managedType ;
592
596
if ( mappingType .hasIdentifierProperty () ) {
593
597
final Property declaredIdentifierProperty = mappingType .getDeclaredIdentifierProperty ();
594
598
if ( declaredIdentifierProperty != null ) {
@@ -611,8 +615,8 @@ else if ( mappingType.getIdentifierMapper() != null ) {
611
615
private <X > void applyVersionAttribute (PersistentClass persistentClass , EntityDomainType <X > jpaEntityType ) {
612
616
final Property declaredVersion = persistentClass .getDeclaredVersion ();
613
617
if ( declaredVersion != null ) {
614
- @ SuppressWarnings ( "unchecked" )
615
- final var attributeContainer = (AttributeContainer <X >) jpaEntityType ;
618
+ final var managedType = ( ManagedDomainType < X >) jpaEntityType ;
619
+ final var attributeContainer = (AttributeContainer <X >) managedType ;
616
620
attributeContainer .getInFlightAccess ()
617
621
.applyVersionAttribute ( attributeFactory .buildVersionAttribute ( jpaEntityType , declaredVersion ) );
618
622
}
@@ -621,22 +625,22 @@ private <X> void applyVersionAttribute(PersistentClass persistentClass, EntityDo
621
625
private <X > void applyVersionAttribute (MappedSuperclass mappingType , MappedSuperclassDomainType <X > jpaMappingType ) {
622
626
final Property declaredVersion = mappingType .getDeclaredVersion ();
623
627
if ( declaredVersion != null ) {
624
- @ SuppressWarnings ( "unchecked" )
625
- final var attributeContainer = (AttributeContainer <X >) jpaMappingType ;
628
+ final var managedType = ( ManagedDomainType < X >) jpaMappingType ;
629
+ final var attributeContainer = (AttributeContainer <X >) managedType ;
626
630
attributeContainer .getInFlightAccess ()
627
631
.applyVersionAttribute ( attributeFactory .buildVersionAttribute ( jpaMappingType , declaredVersion ) );
628
632
}
629
633
}
630
634
631
635
private <X > void applyGenericProperties (PersistentClass persistentClass , EntityDomainType <X > entityType ) {
632
- MappedSuperclass mappedSuperclass = getMappedSuperclass ( persistentClass );
636
+ var mappedSuperclass = getMappedSuperclass ( persistentClass );
633
637
while ( mappedSuperclass != null ) {
634
638
for ( Property superclassProperty : mappedSuperclass .getDeclaredProperties () ) {
635
639
if ( superclassProperty .isGeneric () ) {
636
640
final Property property = persistentClass .getProperty ( superclassProperty .getName () );
637
641
final PersistentAttribute <X , ?> attribute = attributeFactory .buildAttribute ( entityType , property );
638
- @ SuppressWarnings ( "unchecked" )
639
- final var attributeContainer = (AttributeContainer <X >) entityType ;
642
+ final var managedType = ( ManagedDomainType < X >) entityType ;
643
+ final var attributeContainer = (AttributeContainer <X >) managedType ;
640
644
attributeContainer .getInFlightAccess ().addConcreteGenericAttribute ( attribute );
641
645
}
642
646
}
@@ -646,7 +650,7 @@ private <X> void applyGenericProperties(PersistentClass persistentClass, EntityD
646
650
647
651
private MappedSuperclass getMappedSuperclass (PersistentClass persistentClass ) {
648
652
while ( persistentClass != null ) {
649
- final MappedSuperclass mappedSuperclass = persistentClass .getSuperMappedSuperclass ();
653
+ final var mappedSuperclass = persistentClass .getSuperMappedSuperclass ();
650
654
if ( mappedSuperclass != null ) {
651
655
return mappedSuperclass ;
652
656
}
@@ -656,7 +660,7 @@ private MappedSuperclass getMappedSuperclass(PersistentClass persistentClass) {
656
660
}
657
661
658
662
private MappedSuperclass getMappedSuperclass (MappedSuperclass mappedSuperclass ) {
659
- final MappedSuperclass superMappedSuperclass = mappedSuperclass .getSuperMappedSuperclass ();
663
+ final var superMappedSuperclass = mappedSuperclass .getSuperMappedSuperclass ();
660
664
return superMappedSuperclass == null
661
665
? getMappedSuperclass ( mappedSuperclass .getSuperPersistentClass () )
662
666
: superMappedSuperclass ;
@@ -710,7 +714,7 @@ private <X> void populateStaticMetamodel(ManagedDomainType<X> managedType, Set<S
710
714
// todo : this does not account for @MappedSuperclass, mainly
711
715
// because this is not being tracked in our internal
712
716
// metamodel as populated from the annotations properly
713
- final ManagedDomainType <? super X > superType = managedType .getSuperType ();
717
+ final var superType = managedType .getSuperType ();
714
718
if ( superType != null ) {
715
719
populateStaticMetamodel ( superType , processedMetamodelClassName );
716
720
}
@@ -753,7 +757,7 @@ public Class<?> metamodelClass(ManagedDomainType<?> managedDomainType) {
753
757
754
758
private <X > void registerAttributes (Class <?> metamodelClass , ManagedDomainType <X > managedType ) {
755
759
// push the attributes on to the metamodel class...
756
- for ( Attribute < X , ?> attribute : managedType .getDeclaredAttributes () ) {
760
+ for ( var attribute : managedType .getDeclaredAttributes () ) {
757
761
registerAttribute ( metamodelClass , attribute );
758
762
}
759
763
@@ -767,7 +771,7 @@ private <X> void registerAttributes(Class<?> metamodelClass, ManagedDomainType<X
767
771
if ( entityType .hasIdClass () ) {
768
772
final var attributes = entityType .getIdClassAttributesSafely ();
769
773
if ( attributes != null ) {
770
- for ( SingularAttribute <? super X , ?> attribute : attributes ) {
774
+ for ( var attribute : attributes ) {
771
775
registerAttribute ( metamodelClass , attribute );
772
776
}
773
777
}
@@ -789,13 +793,12 @@ private <X> void registerAttribute(Class<?> metamodelClass, Attribute<X, ?> attr
789
793
final boolean allowNonDeclaredFieldReference =
790
794
attribute .getPersistentAttributeType () == Attribute .PersistentAttributeType .EMBEDDED
791
795
|| attribute .getDeclaringType ().getPersistenceType () == Type .PersistenceType .EMBEDDABLE ;
792
-
793
796
injectField ( metamodelClass , name , attribute , allowNonDeclaredFieldReference );
794
797
}
795
798
catch (NoSuchFieldException e ) {
796
799
log .unableToLocateStaticMetamodelField ( metamodelClass .getName (), name );
797
800
// throw new AssertionFailure(
798
- // "Unable to locate static metamodel field : " + metamodelClass.getName() + '#' + name
801
+ // "Unable to locate static metamodel field: " + metamodelClass.getName() + '#' + name
799
802
// );
800
803
}
801
804
}
@@ -836,7 +839,7 @@ public Set<MappedSuperclass> getUnusedMappedSuperclasses() {
836
839
837
840
public <J > BasicDomainType <J > resolveBasicType (Class <J > javaType ) {
838
841
@ SuppressWarnings ("unchecked" )
839
- final BasicDomainType < J > domainType = (BasicDomainType <J >) basicDomainTypeMap .get ( javaType );
842
+ final var domainType = (BasicDomainType <J >) basicDomainTypeMap .get ( javaType );
840
843
if ( domainType == null ) {
841
844
// we cannot use getTypeConfiguration().standardBasicTypeForJavaType(javaType)
842
845
// because that doesn't return the right thing for primitive types
@@ -849,59 +852,55 @@ public <J> BasicDomainType<J> resolveBasicType(Class<J> javaType) {
849
852
}
850
853
851
854
private <J > BasicDomainType <J > basicDomainType (Class <J > javaType ) {
852
- final JavaType <J > javaTypeDescriptor =
853
- getTypeConfiguration ().getJavaTypeRegistry ().resolveDescriptor ( javaType );
855
+ final JavaType <J > javaTypeDescriptor = getJavaTypeRegistry ().resolveDescriptor ( javaType );
854
856
final JdbcType jdbcType =
855
857
javaTypeDescriptor .getRecommendedJdbcType ( typeConfiguration .getCurrentBaseSqlTypeIndicators () );
856
858
return javaType .isPrimitive ()
857
859
? new PrimitiveBasicTypeImpl <>( javaTypeDescriptor , jdbcType , javaType )
858
860
: new BasicTypeImpl <>( javaTypeDescriptor , jdbcType );
859
861
}
860
862
863
+ @ SuppressWarnings ("unchecked" )
861
864
public <J > EmbeddableDomainType <J > locateEmbeddable (Class <J > embeddableClass , Component component ) {
862
- //noinspection unchecked
863
- EmbeddableDomainType <J > domainType = (EmbeddableDomainType <J >) embeddables .get ( embeddableClass );
864
- if ( domainType == null ) {
865
- final List <EmbeddableDomainType <?>> embeddableDomainTypes = embeddablesToProcess .get ( embeddableClass );
865
+ final var domainType = (EmbeddableDomainType <J >) embeddables .get ( embeddableClass );
866
+ if ( domainType != null ) {
867
+ return domainType ;
868
+ }
869
+ else {
870
+ final var embeddableDomainTypes = embeddablesToProcess .get ( embeddableClass );
866
871
if ( embeddableDomainTypes != null ) {
867
- for ( EmbeddableDomainType <?> embeddableDomainType : embeddableDomainTypes ) {
872
+ for ( var embeddableDomainType : embeddableDomainTypes ) {
868
873
final Component cachedComponent = componentByEmbeddable .get ( embeddableDomainType );
869
874
if ( cachedComponent .isSame ( component ) ) {
870
- //noinspection unchecked
871
- domainType = (EmbeddableDomainType <J >) embeddableDomainType ;
872
- break ;
875
+ return (EmbeddableDomainType <J >) embeddableDomainType ;
873
876
}
874
877
else if ( cachedComponent .getComponentClass ().equals ( component .getComponentClass () ) ) {
875
878
final int cachedComponentPropertySpan = cachedComponent .getPropertySpan ();
876
879
if ( cachedComponentPropertySpan != component .getPropertySpan () ) {
877
- throw new MappingException (
878
- "Encountered multiple component mappings for the same java class "
880
+ throw new MappingException ( "Encountered multiple component mappings for the same java class "
879
881
+ embeddableClass .getName () +
880
882
" with different property mappings. Every property mapping combination should have its own java class" );
881
883
}
882
884
else {
883
885
for ( int i = 0 ; i < cachedComponentPropertySpan ; i ++ ) {
884
886
if ( !cachedComponent .getProperty ( i ).getName ()
885
887
.equals ( component .getProperty ( i ).getName () ) ) {
886
- throw new MappingException (
887
- "Encountered multiple component mappings for the same java class "
888
+ throw new MappingException ( "Encountered multiple component mappings for the same java class "
888
889
+ embeddableClass .getName () +
889
890
" with different property mappings. Every property mapping combination should have its own java class" );
890
891
}
891
892
}
892
893
}
893
- //noinspection unchecked
894
- domainType = (EmbeddableDomainType <J >) embeddableDomainType ;
895
- break ;
894
+ return (EmbeddableDomainType <J >) embeddableDomainType ;
896
895
}
897
896
else {
898
897
throw new MappingException ( "Encountered multiple component mappings for the same java class "
899
- + embeddableClass .getName () +
900
- " with different property mappings. Every property mapping combination should have its own java class" );
898
+ + embeddableClass .getName () +
899
+ " with different property mappings. Every property mapping combination should have its own java class" );
901
900
}
902
901
}
903
902
}
903
+ return null ;
904
904
}
905
- return domainType ;
906
905
}
907
906
}
0 commit comments