21
21
22
22
import org .checkerframework .checker .nullness .qual .Nullable ;
23
23
24
+ import org .hibernate .AnnotationException ;
24
25
import org .hibernate .boot .model .NamedEntityGraphDefinition ;
25
26
import org .hibernate .boot .query .NamedQueryDefinition ;
26
27
import org .hibernate .boot .registry .classloading .spi .ClassLoaderService ;
@@ -501,7 +502,7 @@ private void applyNamedEntityGraphs(Collection<NamedEntityGraphDefinition> named
501
502
}
502
503
503
504
final NamedEntityGraph namedEntityGraph = definition .getAnnotation ();
504
- final RootGraphImpl <?> entityGraph =
505
+ final RootGraphImplementor <?> entityGraph =
505
506
createEntityGraph (
506
507
namedEntityGraph ,
507
508
definition .getRegisteredName (),
@@ -513,24 +514,26 @@ private void applyNamedEntityGraphs(Collection<NamedEntityGraphDefinition> named
513
514
}
514
515
}
515
516
516
- private <T > RootGraphImpl <T > createEntityGraph (
517
+ private <T > RootGraphImplementor <T > createEntityGraph (
517
518
NamedEntityGraph namedEntityGraph ,
518
519
String registeredName ,
519
520
EntityDomainType <T > entityType ,
520
521
boolean includeAllAttributes ) {
521
- final RootGraphImpl <T > entityGraph =
522
+ final RootGraphImplementor <T > entityGraph =
522
523
createRootGraph ( registeredName , entityType , includeAllAttributes );
523
524
524
525
if ( namedEntityGraph .subclassSubgraphs () != null ) {
525
- for ( var subclassSubgraph : namedEntityGraph .subclassSubgraphs () ) {
526
- GraphImplementor <?> subgraph = (GraphImplementor <?>) entityGraph .addTreatedSubgraph (
527
- (Class ) subclassSubgraph .type () );
528
-
529
- applyNamedAttributeNodes (
530
- subclassSubgraph .attributeNodes (),
531
- namedEntityGraph ,
532
- subgraph
533
- );
526
+ for ( NamedSubgraph subclassSubgraph : namedEntityGraph .subclassSubgraphs () ) {
527
+ final Class <?> subgraphType = subclassSubgraph .type ();
528
+ final Class <T > graphJavaType = entityGraph .getGraphedType ().getJavaType ();
529
+ if ( !graphJavaType .isAssignableFrom ( subgraphType ) ) {
530
+ throw new AnnotationException ( "Named subgraph type '" + subgraphType .getName ()
531
+ + "' is not a subtype of the graph type '" + graphJavaType .getName () + "'" );
532
+ }
533
+ @ SuppressWarnings ("unchecked" ) // Safe, because we just checked
534
+ final Class <? extends T > subtype = (Class <? extends T >) subgraphType ;
535
+ final GraphImplementor <? extends T > subgraph = entityGraph .addTreatedSubgraph ( subtype );
536
+ applyNamedAttributeNodes ( subclassSubgraph .attributeNodes (), namedEntityGraph , subgraph );
534
537
}
535
538
}
536
539
@@ -541,7 +544,7 @@ private <T> RootGraphImpl<T> createEntityGraph(
541
544
return entityGraph ;
542
545
}
543
546
544
- private static <T > RootGraphImpl <T > createRootGraph (
547
+ private static <T > RootGraphImplementor <T > createRootGraph (
545
548
String name , EntityDomainType <T > entityType , boolean includeAllAttributes ) {
546
549
final RootGraphImpl <T > entityGraph = new RootGraphImpl <>( name , entityType );
547
550
if ( includeAllAttributes ) {
@@ -558,8 +561,8 @@ private void applyNamedAttributeNodes(
558
561
GraphImplementor <?> graphNode ) {
559
562
for ( NamedAttributeNode namedAttributeNode : namedAttributeNodes ) {
560
563
final String value = namedAttributeNode .value ();
561
- final AttributeNodeImplementor <?> attributeNode = ( AttributeNodeImplementor <?>) graphNode . addAttributeNode (
562
- value );
564
+ final AttributeNodeImplementor <?> attributeNode =
565
+ ( AttributeNodeImplementor <?>) graphNode . addAttributeNode ( value );
563
566
564
567
if ( isNotEmpty ( namedAttributeNode .subgraph () ) ) {
565
568
applyNamedSubgraphs (
@@ -580,43 +583,36 @@ private void applyNamedAttributeNodes(
580
583
}
581
584
}
582
585
583
- @ SuppressWarnings ({ "unchecked" , "rawtypes" })
584
586
private <T > void applyNamedSubgraphs (
585
587
NamedEntityGraph namedEntityGraph ,
586
588
String subgraphName ,
587
- AttributeNodeImplementor <T > attributeNode , Boolean isKeySubGraph ) {
589
+ AttributeNodeImplementor <T > attributeNode ,
590
+ boolean isKeySubGraph ) {
588
591
for ( NamedSubgraph namedSubgraph : namedEntityGraph .subgraphs () ) {
589
592
if ( subgraphName .equals ( namedSubgraph .name () ) ) {
590
- final var isDefaultSubgraphType = namedSubgraph .type ().equals ( void .class );
591
- final Class subGraphType = isDefaultSubgraphType ? null : namedSubgraph .type ();
592
-
593
- final SubGraphImplementor <?> subgraph = makeAttributeNodeSubgraph (
594
- attributeNode , isKeySubGraph ,
595
- subGraphType
596
- );
597
-
598
- applyNamedAttributeNodes (
599
- namedSubgraph .attributeNodes (),
600
- namedEntityGraph ,
601
- subgraph
602
- );
593
+ final boolean isDefaultSubgraphType = namedSubgraph .type ().equals ( void .class );
594
+ final Class <?> subGraphType = isDefaultSubgraphType ? null : namedSubgraph .type ();
595
+ final SubGraphImplementor <?> subgraph =
596
+ makeAttributeNodeSubgraph ( attributeNode , isKeySubGraph , subGraphType );
597
+ applyNamedAttributeNodes ( namedSubgraph .attributeNodes (), namedEntityGraph , subgraph );
603
598
}
604
599
}
605
600
}
606
601
607
602
private static <T > SubGraphImplementor <?> makeAttributeNodeSubgraph (
608
603
AttributeNodeImplementor <T > attributeNode ,
609
- Boolean isKeySubGraph ,
610
- Class <T > subGraphType ) {
611
-
604
+ boolean isKeySubGraph ,
605
+ Class <?> subGraphType ) {
612
606
if ( isKeySubGraph ) {
613
- return subGraphType != null ? attributeNode .makeKeySubGraph ( subGraphType )
607
+ return subGraphType != null
608
+ ? attributeNode .makeKeySubGraph ( subGraphType )
614
609
: attributeNode .makeKeySubGraph ();
615
610
}
616
-
617
- return subGraphType != null ?
618
- attributeNode .makeSubGraph ( subGraphType ) :
619
- attributeNode .makeSubGraph ();
611
+ else {
612
+ return subGraphType != null
613
+ ? attributeNode .makeSubGraph ( subGraphType )
614
+ : attributeNode .makeSubGraph ();
615
+ }
620
616
}
621
617
622
618
private <X > Class <X > resolveRequestedClass (String entityName ) {
@@ -783,30 +779,11 @@ public void processJpa(
783
779
784
780
private void populateStaticMetamodel (MetadataImplementor bootMetamodel , MetadataContext context ) {
785
781
bootMetamodel .visitNamedHqlQueryDefinitions ( definition
786
- -> injectTypedQueryReference (
787
- definition ,
788
- namedQueryMetamodelClass (
789
- definition ,
790
- context
791
- )
792
- ) );
782
+ -> injectTypedQueryReference ( definition , namedQueryMetamodelClass ( definition , context ) ) );
793
783
bootMetamodel .visitNamedNativeQueryDefinitions ( definition
794
- -> injectTypedQueryReference (
795
- definition ,
796
- namedQueryMetamodelClass (
797
- definition ,
798
- context
799
- )
800
- ) );
784
+ -> injectTypedQueryReference ( definition , namedQueryMetamodelClass ( definition , context ) ) );
801
785
bootMetamodel .getNamedEntityGraphs ().values ().forEach ( definition
802
- -> injectEntityGraph (
803
- definition ,
804
- graphMetamodelClass (
805
- definition ,
806
- context
807
- ),
808
- this
809
- ) );
786
+ -> injectEntityGraph ( definition , graphMetamodelClass ( definition , context ), this ) );
810
787
}
811
788
812
789
private Class <?> namedQueryMetamodelClass (NamedQueryDefinition <?> definition , MetadataContext context ) {
0 commit comments