5
5
package org .hibernate .metamodel .internal ;
6
6
7
7
import jakarta .persistence .metamodel .Attribute ;
8
- import jakarta .persistence .metamodel .IdentifiableType ;
9
8
import jakarta .persistence .metamodel .SingularAttribute ;
10
9
import jakarta .persistence .metamodel .Type ;
11
10
import org .hibernate .AssertionFailure ;
17
16
import org .hibernate .internal .CoreLogging ;
18
17
import org .hibernate .internal .CoreMessageLogger ;
19
18
import org .hibernate .internal .util .collections .ArrayHelper ;
20
- import org .hibernate .internal .util .collections .CollectionHelper ;
21
19
import org .hibernate .mapping .Component ;
22
20
import org .hibernate .mapping .MappedSuperclass ;
23
21
import org .hibernate .mapping .PersistentClass ;
56
54
import java .util .function .BiFunction ;
57
55
58
56
import static java .util .Collections .unmodifiableMap ;
57
+ import static org .hibernate .internal .util .collections .CollectionHelper .mapOfSize ;
59
58
import static org .hibernate .metamodel .internal .InjectionHelper .injectField ;
60
59
61
60
/**
@@ -158,17 +157,11 @@ public Set<EmbeddableDomainType<?>> getEmbeddableTypeSet() {
158
157
159
158
public Map <Class <?>, MappedSuperclassDomainType <?>> getMappedSuperclassTypeMap () {
160
159
// we need to actually build this map...
161
- final Map <Class <?>, MappedSuperclassDomainType <?>> mappedSuperClassTypeMap = CollectionHelper .mapOfSize (
162
- mappedSuperclassByMappedSuperclassMapping .size ()
163
- );
164
-
165
- for ( MappedSuperclassDomainType <?> mappedSuperclassType : mappedSuperclassByMappedSuperclassMapping .values () ) {
166
- mappedSuperClassTypeMap .put (
167
- mappedSuperclassType .getJavaType (),
168
- mappedSuperclassType
169
- );
160
+ final Map <Class <?>, MappedSuperclassDomainType <?>> mappedSuperClassTypeMap =
161
+ mapOfSize ( mappedSuperclassByMappedSuperclassMapping .size () );
162
+ for ( var mappedSuperclassType : mappedSuperclassByMappedSuperclassMapping .values () ) {
163
+ mappedSuperClassTypeMap .put ( mappedSuperclassType .getJavaType (), mappedSuperclassType );
170
164
}
171
-
172
165
return mappedSuperClassTypeMap ;
173
166
}
174
167
@@ -263,12 +256,12 @@ public Map<String, IdentifiableDomainType<?>> getIdentifiableTypesByName() {
263
256
Property property ,
264
257
IdentifiableDomainType <X > entityType ,
265
258
BiFunction <IdentifiableDomainType <X >, Property , PersistentAttribute <X , ?>> factoryFunction ) {
266
- final Component component = property .getValue () instanceof Component comp ? comp : null ;
267
- if ( component != null && component .isGeneric () ) {
259
+ if ( property .getValue () instanceof Component component && component .isGeneric () ) {
268
260
// This is an embeddable property that uses generics, we have to retrieve the generic
269
261
// component previously registered and create the concrete attribute
270
- final Component genericComponent = runtimeModelCreationContext .getMetadata ()
271
- .getGenericComponent ( component .getComponentClass () );
262
+ final Component genericComponent =
263
+ runtimeModelCreationContext .getMetadata ()
264
+ .getGenericComponent ( component .getComponentClass () );
272
265
final Property genericProperty = property .copy ();
273
266
genericProperty .setValue ( genericComponent );
274
267
genericProperty .setGeneric ( true );
@@ -382,8 +375,9 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
382
375
383
376
384
377
while ( ! embeddablesToProcess .isEmpty () ) {
385
- final ArrayList <EmbeddableDomainType <?>> processingEmbeddables = new ArrayList <>( embeddablesToProcess .size () );
386
- for ( List <EmbeddableDomainType <?>> embeddableDomainTypes : embeddablesToProcess .values () ) {
378
+ final List <EmbeddableDomainType <?>> processingEmbeddables =
379
+ new ArrayList <>( embeddablesToProcess .size () );
380
+ for ( var embeddableDomainTypes : embeddablesToProcess .values () ) {
387
381
processingEmbeddables .addAll ( embeddableDomainTypes );
388
382
}
389
383
@@ -403,7 +397,6 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
403
397
// generic component embeddables used just for concrete type resolution
404
398
if ( !component .isGeneric () && !( embeddable .getExpressibleJavaType () instanceof EntityJavaType <?> ) ) {
405
399
embeddables .put ( embeddable .getJavaType (), embeddable );
406
-
407
400
if ( staticMetamodelScanEnabled ) {
408
401
populateStaticMetamodel ( embeddable , processedMetamodelClasses );
409
402
}
@@ -414,27 +407,24 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
414
407
415
408
private static boolean isIdentifierProperty (Property property , MappedSuperclass mappedSuperclass ) {
416
409
final Component identifierMapper = mappedSuperclass .getIdentifierMapper ();
417
- return identifierMapper != null && ArrayHelper .contains (
418
- identifierMapper .getPropertyNames (),
419
- property .getName ()
420
- );
410
+ return identifierMapper != null
411
+ && ArrayHelper .contains ( identifierMapper .getPropertyNames (), property .getName () );
421
412
}
422
413
423
414
private <T > void addAttribute (EmbeddableDomainType <T > embeddable , Property property , Component component ) {
424
415
final PersistentAttribute <T , ?> attribute =
425
416
attributeFactory .buildAttribute ( embeddable , property );
426
417
if ( attribute != null ) {
427
- final Property superclassProperty = getMappedSuperclassProperty (
428
- property .getName (),
429
- component .getMappedSuperclass ()
430
- );
418
+ final Property superclassProperty =
419
+ getMappedSuperclassProperty ( property .getName (),
420
+ component .getMappedSuperclass () );
431
421
if ( superclassProperty != null && superclassProperty .isGeneric () ) {
432
422
@ SuppressWarnings ("unchecked" )
433
- final AttributeContainer < T > attributeContainer = (AttributeContainer <T >) embeddable ;
423
+ final var attributeContainer = (AttributeContainer <T >) embeddable ;
434
424
attributeContainer .getInFlightAccess ().addConcreteGenericAttribute ( attribute );
435
425
}
436
426
else {
437
- addAttribute (embeddable , attribute );
427
+ addAttribute ( embeddable , attribute );
438
428
}
439
429
}
440
430
}
@@ -456,8 +446,9 @@ private <T> void addAttribute(ManagedDomainType<T> type, PersistentAttribute<T,
456
446
@ SuppressWarnings ("unchecked" )
457
447
final AttributeContainer <T > container = (AttributeContainer <T >) type ;
458
448
final AttributeContainer .InFlightAccess <T > inFlightAccess = container .getInFlightAccess ();
459
- final boolean virtual = attribute .getPersistentAttributeType () == Attribute .PersistentAttributeType .EMBEDDED
460
- && attribute .getAttributeJavaType () instanceof EntityJavaType <?>;
449
+ final boolean virtual =
450
+ attribute .getPersistentAttributeType () == Attribute .PersistentAttributeType .EMBEDDED
451
+ && attribute .getAttributeJavaType () instanceof EntityJavaType <?>;
461
452
if ( virtual ) {
462
453
@ SuppressWarnings ("unchecked" )
463
454
final EmbeddableDomainType <T > embeddableDomainType =
@@ -487,24 +478,19 @@ private <T> void applyIdMetadata(PersistentClass persistentClass, IdentifiableDo
487
478
@ SuppressWarnings ("unchecked" )
488
479
final AttributeContainer <T > attributeContainer = (AttributeContainer <T >) identifiableType ;
489
480
if ( declaredIdentifierProperty != null ) {
490
- final SingularPersistentAttribute < T , ?> idAttribute =
481
+ final var idAttribute =
491
482
(SingularPersistentAttribute <T , ?>)
492
- buildAttribute (
493
- declaredIdentifierProperty ,
494
- identifiableType ,
495
- attributeFactory ::buildIdAttribute
496
- );
483
+ buildAttribute ( declaredIdentifierProperty , identifiableType ,
484
+ attributeFactory ::buildIdAttribute );
497
485
attributeContainer .getInFlightAccess ().applyIdAttribute ( idAttribute );
498
486
}
499
487
else {
500
488
final Property superclassIdentifier = getMappedSuperclassIdentifier ( persistentClass );
501
489
if ( superclassIdentifier != null && superclassIdentifier .isGeneric () ) {
502
490
// If the superclass identifier is generic we have to build the attribute to register the concrete type
503
- final SingularPersistentAttribute <T , ?> concreteIdentifier =
504
- attributeFactory .buildIdAttribute (
505
- identifiableType ,
506
- persistentClass .getIdentifierProperty ()
507
- );
491
+ final var concreteIdentifier =
492
+ attributeFactory .buildIdAttribute ( identifiableType ,
493
+ persistentClass .getIdentifierProperty () );
508
494
attributeContainer .getInFlightAccess ().addConcreteGenericAttribute ( concreteIdentifier );
509
495
}
510
496
}
@@ -542,7 +528,7 @@ private <T> void applyIdMetadata(PersistentClass persistentClass, IdentifiableDo
542
528
final IdentifiableDomainType <?> idDomainType =
543
529
identifiableTypesByName .get ( compositeId .getOwner ().getEntityName () );
544
530
@ SuppressWarnings ("unchecked" )
545
- final AbstractIdentifiableType < T > idType = (AbstractIdentifiableType <T >) idDomainType ;
531
+ final var idType = (AbstractIdentifiableType <T >) idDomainType ;
546
532
applyIdAttributes ( identifiableType , idType , propertySpan , cidProperties , idClassType );
547
533
}
548
534
}
@@ -562,7 +548,7 @@ private <T> void applyIdAttributes(
562
548
}
563
549
564
550
@ SuppressWarnings ("unchecked" )
565
- final AttributeContainer < T > container = (AttributeContainer <T >) identifiableType ;
551
+ final var container = (AttributeContainer <T >) identifiableType ;
566
552
container .getInFlightAccess ().applyNonAggregatedIdAttributes ( idAttributes , idClassType );
567
553
}
568
554
@@ -579,54 +565,45 @@ private Property getMappedSuperclassIdentifier(PersistentClass persistentClass)
579
565
}
580
566
581
567
private <Y > EmbeddableTypeImpl <Y > applyIdClassMetadata (Component idClassComponent ) {
582
- final JavaType <Y > javaType =
583
- getTypeConfiguration ().getJavaTypeRegistry ()
584
- .resolveManagedTypeDescriptor ( idClassComponent .getComponentClass () );
585
-
586
- final MappedSuperclass mappedSuperclass = idClassComponent .getMappedSuperclass ();
587
- final MappedSuperclassDomainType <? super Y > superType ;
588
- if ( mappedSuperclass != null ) {
589
- //noinspection unchecked
590
- superType = (MappedSuperclassDomainType <? super Y >) locateMappedSuperclassType ( mappedSuperclass );
591
- }
592
- else {
593
- superType = null ;
594
- }
595
-
596
- final EmbeddableTypeImpl <Y > embeddableType = new EmbeddableTypeImpl <>(
597
- javaType ,
598
- superType ,
599
- null ,
600
- false ,
601
- getJpaMetamodel ()
602
- );
568
+ final EmbeddableTypeImpl <Y > embeddableType =
569
+ new EmbeddableTypeImpl <>(
570
+ getTypeConfiguration ().getJavaTypeRegistry ()
571
+ .resolveManagedTypeDescriptor ( idClassComponent .getComponentClass () ),
572
+ getMappedSuperclassDomainType ( idClassComponent ),
573
+ null ,
574
+ false ,
575
+ getJpaMetamodel ()
576
+ );
603
577
registerEmbeddableType ( embeddableType , idClassComponent );
604
578
return embeddableType ;
605
579
}
606
580
581
+ @ SuppressWarnings ("unchecked" )
582
+ private <Y > MappedSuperclassDomainType <? super Y > getMappedSuperclassDomainType (Component idClassComponent ) {
583
+ final MappedSuperclass mappedSuperclass = idClassComponent .getMappedSuperclass ();
584
+ return mappedSuperclass == null ? null
585
+ : (MappedSuperclassDomainType <? super Y >)
586
+ locateMappedSuperclassType ( mappedSuperclass );
587
+ }
588
+
607
589
private <X > void applyIdMetadata (MappedSuperclass mappingType , MappedSuperclassDomainType <X > jpaMappingType ) {
608
590
@ SuppressWarnings ("unchecked" )
609
- final AttributeContainer < X > attributeContainer = (AttributeContainer <X >) jpaMappingType ;
591
+ final var attributeContainer = (AttributeContainer <X >) jpaMappingType ;
610
592
if ( mappingType .hasIdentifierProperty () ) {
611
593
final Property declaredIdentifierProperty = mappingType .getDeclaredIdentifierProperty ();
612
594
if ( declaredIdentifierProperty != null ) {
613
- final SingularPersistentAttribute < X , ?> attribute =
595
+ final var attribute =
614
596
(SingularPersistentAttribute <X , ?>)
615
- buildAttribute (
616
- declaredIdentifierProperty ,
617
- jpaMappingType ,
618
- attributeFactory ::buildIdAttribute
619
- );
597
+ buildAttribute ( declaredIdentifierProperty , jpaMappingType ,
598
+ attributeFactory ::buildIdAttribute );
620
599
attributeContainer .getInFlightAccess ().applyIdAttribute ( attribute );
621
600
}
622
601
}
623
602
//a MappedSuperclass can have no identifier if the id is set below in the hierarchy
624
603
else if ( mappingType .getIdentifierMapper () != null ) {
625
- final Set <SingularPersistentAttribute <? super X , ?>> attributes =
626
- buildIdClassAttributes (
627
- jpaMappingType ,
628
- mappingType .getIdentifierMapper ().getProperties ()
629
- );
604
+ final var attributes =
605
+ buildIdClassAttributes ( jpaMappingType ,
606
+ mappingType .getIdentifierMapper ().getProperties () );
630
607
attributeContainer .getInFlightAccess ().applyIdClassAttributes ( attributes );
631
608
}
632
609
}
@@ -635,21 +612,19 @@ private <X> void applyVersionAttribute(PersistentClass persistentClass, EntityDo
635
612
final Property declaredVersion = persistentClass .getDeclaredVersion ();
636
613
if ( declaredVersion != null ) {
637
614
@ SuppressWarnings ("unchecked" )
638
- final AttributeContainer <X > attributeContainer = (AttributeContainer <X >) jpaEntityType ;
639
- attributeContainer .getInFlightAccess ().applyVersionAttribute (
640
- attributeFactory .buildVersionAttribute ( jpaEntityType , declaredVersion )
641
- );
615
+ final var attributeContainer = (AttributeContainer <X >) jpaEntityType ;
616
+ attributeContainer .getInFlightAccess ()
617
+ .applyVersionAttribute ( attributeFactory .buildVersionAttribute ( jpaEntityType , declaredVersion ) );
642
618
}
643
619
}
644
620
645
621
private <X > void applyVersionAttribute (MappedSuperclass mappingType , MappedSuperclassDomainType <X > jpaMappingType ) {
646
622
final Property declaredVersion = mappingType .getDeclaredVersion ();
647
623
if ( declaredVersion != null ) {
648
624
@ SuppressWarnings ("unchecked" )
649
- final AttributeContainer <X > xAttributeContainer = (AttributeContainer <X >) jpaMappingType ;
650
- xAttributeContainer .getInFlightAccess ().applyVersionAttribute (
651
- attributeFactory .buildVersionAttribute ( jpaMappingType , declaredVersion )
652
- );
625
+ final var attributeContainer = (AttributeContainer <X >) jpaMappingType ;
626
+ attributeContainer .getInFlightAccess ()
627
+ .applyVersionAttribute ( attributeFactory .buildVersionAttribute ( jpaMappingType , declaredVersion ) );
653
628
}
654
629
}
655
630
@@ -661,7 +636,7 @@ private <X> void applyGenericProperties(PersistentClass persistentClass, EntityD
661
636
final Property property = persistentClass .getProperty ( superclassProperty .getName () );
662
637
final PersistentAttribute <X , ?> attribute = attributeFactory .buildAttribute ( entityType , property );
663
638
@ SuppressWarnings ("unchecked" )
664
- final AttributeContainer < X > attributeContainer = (AttributeContainer <X >) entityType ;
639
+ final var attributeContainer = (AttributeContainer <X >) entityType ;
665
640
attributeContainer .getInFlightAccess ().addConcreteGenericAttribute ( attribute );
666
641
}
667
642
}
@@ -682,9 +657,9 @@ private MappedSuperclass getMappedSuperclass(PersistentClass persistentClass) {
682
657
683
658
private MappedSuperclass getMappedSuperclass (MappedSuperclass mappedSuperclass ) {
684
659
final MappedSuperclass superMappedSuperclass = mappedSuperclass .getSuperMappedSuperclass ();
685
- return superMappedSuperclass ! = null
686
- ? superMappedSuperclass
687
- : getMappedSuperclass ( mappedSuperclass . getSuperPersistentClass () ) ;
660
+ return superMappedSuperclass = = null
661
+ ? getMappedSuperclass ( mappedSuperclass . getSuperPersistentClass () )
662
+ : superMappedSuperclass ;
688
663
}
689
664
690
665
private Property getMappedSuperclassProperty (String propertyName , MappedSuperclass mappedSuperclass ) {
@@ -731,7 +706,8 @@ private <X> void populateStaticMetamodel(ManagedDomainType<X> managedType, Set<S
731
706
&& processedMetamodelClassName .add ( metamodelClassName ( managedType ) ) ) {
732
707
final Class <?> metamodelClass = metamodelClass ( managedType );
733
708
if ( metamodelClass != null ) {
734
- populateMetamodelClass ( managedType , metamodelClass );
709
+ registerAttributes ( metamodelClass , managedType );
710
+ injectManagedType ( managedType , metamodelClass );
735
711
}
736
712
// todo : this does not account for @MappedSuperclass, mainly
737
713
// because this is not being tracked in our internal
@@ -743,11 +719,6 @@ private <X> void populateStaticMetamodel(ManagedDomainType<X> managedType, Set<S
743
719
}
744
720
}
745
721
746
- private <X > void populateMetamodelClass (ManagedDomainType <X > managedType , Class <?> metamodelClass ) {
747
- registerAttributes ( metamodelClass , managedType );
748
- injectManagedType ( managedType , metamodelClass );
749
- }
750
-
751
722
private static <X > void injectManagedType (ManagedDomainType <X > managedType , Class <?> metamodelClass ) {
752
723
try {
753
724
injectField ( metamodelClass , "class_" , managedType , false );
@@ -788,17 +759,15 @@ private <X> void registerAttributes(Class<?> metamodelClass, ManagedDomainType<X
788
759
registerAttribute ( metamodelClass , attribute );
789
760
}
790
761
791
- if ( managedType instanceof IdentifiableType ) {
792
- final AbstractIdentifiableType <X > entityType = (AbstractIdentifiableType <X >) managedType ;
793
-
762
+ if ( managedType instanceof AbstractIdentifiableType <X > entityType ) {
794
763
// handle version
795
764
if ( entityType .hasDeclaredVersionAttribute () ) {
796
765
registerAttribute ( metamodelClass , entityType .getDeclaredVersion () );
797
766
}
798
767
799
768
// handle id-class mappings specially
800
769
if ( entityType .hasIdClass () ) {
801
- final Set < SingularPersistentAttribute <? super X , ?>> attributes = entityType .getIdClassAttributesSafely ();
770
+ final var attributes = entityType .getIdClassAttributesSafely ();
802
771
if ( attributes != null ) {
803
772
for ( SingularAttribute <? super X , ?> attribute : attributes ) {
804
773
registerAttribute ( metamodelClass , attribute );
@@ -833,7 +802,6 @@ private <X> void registerAttribute(Class<?> metamodelClass, Attribute<X, ?> attr
833
802
}
834
803
}
835
804
836
-
837
805
public MappedSuperclassDomainType <?> locateMappedSuperclassType (MappedSuperclass mappedSuperclass ) {
838
806
return mappedSuperclassByMappedSuperclassMapping .get ( mappedSuperclass );
839
807
}
@@ -874,22 +842,24 @@ public <J> BasicDomainType<J> resolveBasicType(Class<J> javaType) {
874
842
if ( domainType == null ) {
875
843
// we cannot use getTypeConfiguration().standardBasicTypeForJavaType(javaType)
876
844
// because that doesn't return the right thing for primitive types
877
- final JavaType <J > javaTypeDescriptor =
878
- getTypeConfiguration ().getJavaTypeRegistry ().resolveDescriptor ( javaType );
879
- final JdbcType jdbcType =
880
- javaTypeDescriptor .getRecommendedJdbcType ( typeConfiguration .getCurrentBaseSqlTypeIndicators () );
881
- final BasicDomainType <J > type =
882
- javaType .isPrimitive ()
883
- ? new PrimitiveBasicTypeImpl <>( javaTypeDescriptor , jdbcType , javaType )
884
- : new BasicTypeImpl <>( javaTypeDescriptor , jdbcType );
885
- basicDomainTypeMap .put ( javaType , type );
886
- return type ;
845
+ basicDomainTypeMap .put ( javaType , basicDomainType ( javaType ) );
846
+ return basicDomainType ( javaType );
887
847
}
888
848
else {
889
849
return domainType ;
890
850
}
891
851
}
892
852
853
+ private <J > BasicDomainType <J > basicDomainType (Class <J > javaType ) {
854
+ final JavaType <J > javaTypeDescriptor =
855
+ getTypeConfiguration ().getJavaTypeRegistry ().resolveDescriptor ( javaType );
856
+ final JdbcType jdbcType =
857
+ javaTypeDescriptor .getRecommendedJdbcType ( typeConfiguration .getCurrentBaseSqlTypeIndicators () );
858
+ return javaType .isPrimitive ()
859
+ ? new PrimitiveBasicTypeImpl <>( javaTypeDescriptor , jdbcType , javaType )
860
+ : new BasicTypeImpl <>( javaTypeDescriptor , jdbcType );
861
+ }
862
+
893
863
public <J > EmbeddableDomainType <J > locateEmbeddable (Class <J > embeddableClass , Component component ) {
894
864
//noinspection unchecked
895
865
EmbeddableDomainType <J > domainType = (EmbeddableDomainType <J >) embeddables .get ( embeddableClass );
0 commit comments