2121
2222import org .checkerframework .checker .nullness .qual .Nullable ;
2323
24+ import org .hibernate .AnnotationException ;
2425import org .hibernate .boot .model .NamedEntityGraphDefinition ;
2526import org .hibernate .boot .query .NamedQueryDefinition ;
2627import org .hibernate .boot .registry .classloading .spi .ClassLoaderService ;
@@ -501,7 +502,7 @@ private void applyNamedEntityGraphs(Collection<NamedEntityGraphDefinition> named
501502 }
502503
503504 final NamedEntityGraph namedEntityGraph = definition .getAnnotation ();
504- final RootGraphImpl <?> entityGraph =
505+ final RootGraphImplementor <?> entityGraph =
505506 createEntityGraph (
506507 namedEntityGraph ,
507508 definition .getRegisteredName (),
@@ -513,24 +514,26 @@ private void applyNamedEntityGraphs(Collection<NamedEntityGraphDefinition> named
513514 }
514515 }
515516
516- private <T > RootGraphImpl <T > createEntityGraph (
517+ private <T > RootGraphImplementor <T > createEntityGraph (
517518 NamedEntityGraph namedEntityGraph ,
518519 String registeredName ,
519520 EntityDomainType <T > entityType ,
520521 boolean includeAllAttributes ) {
521- final RootGraphImpl <T > entityGraph =
522+ final RootGraphImplementor <T > entityGraph =
522523 createRootGraph ( registeredName , entityType , includeAllAttributes );
523524
524525 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 );
534537 }
535538 }
536539
@@ -541,7 +544,7 @@ private <T> RootGraphImpl<T> createEntityGraph(
541544 return entityGraph ;
542545 }
543546
544- private static <T > RootGraphImpl <T > createRootGraph (
547+ private static <T > RootGraphImplementor <T > createRootGraph (
545548 String name , EntityDomainType <T > entityType , boolean includeAllAttributes ) {
546549 final RootGraphImpl <T > entityGraph = new RootGraphImpl <>( name , entityType );
547550 if ( includeAllAttributes ) {
@@ -558,8 +561,8 @@ private void applyNamedAttributeNodes(
558561 GraphImplementor <?> graphNode ) {
559562 for ( NamedAttributeNode namedAttributeNode : namedAttributeNodes ) {
560563 final String value = namedAttributeNode .value ();
561- final AttributeNodeImplementor <?> attributeNode = ( AttributeNodeImplementor <?>) graphNode . addAttributeNode (
562- value );
564+ final AttributeNodeImplementor <?> attributeNode =
565+ ( AttributeNodeImplementor <?>) graphNode . addAttributeNode ( value );
563566
564567 if ( isNotEmpty ( namedAttributeNode .subgraph () ) ) {
565568 applyNamedSubgraphs (
@@ -580,20 +583,18 @@ private void applyNamedAttributeNodes(
580583 }
581584 }
582585
583- @ SuppressWarnings ({ "unchecked" , "rawtypes" })
586+ // @SuppressWarnings({ "unchecked", "rawtypes" })
584587 private <T > void applyNamedSubgraphs (
585588 NamedEntityGraph namedEntityGraph ,
586589 String subgraphName ,
587590 AttributeNodeImplementor <T > attributeNode , Boolean isKeySubGraph ) {
588591 for ( NamedSubgraph namedSubgraph : namedEntityGraph .subgraphs () ) {
589592 if ( subgraphName .equals ( namedSubgraph .name () ) ) {
590- final var isDefaultSubgraphType = namedSubgraph .type ().equals ( void .class );
591- final Class subGraphType = isDefaultSubgraphType ? null : namedSubgraph .type ();
593+ final boolean isDefaultSubgraphType = namedSubgraph .type ().equals ( void .class );
594+ final Class <?> subGraphType = isDefaultSubgraphType ? null : namedSubgraph .type ();
592595
593- final SubGraphImplementor <?> subgraph = makeAttributeNodeSubgraph (
594- attributeNode , isKeySubGraph ,
595- subGraphType
596- );
596+ final SubGraphImplementor <?> subgraph =
597+ makeAttributeNodeSubgraph ( attributeNode , isKeySubGraph , subGraphType );
597598
598599 applyNamedAttributeNodes (
599600 namedSubgraph .attributeNodes (),
@@ -607,16 +608,16 @@ private <T> void applyNamedSubgraphs(
607608 private static <T > SubGraphImplementor <?> makeAttributeNodeSubgraph (
608609 AttributeNodeImplementor <T > attributeNode ,
609610 Boolean isKeySubGraph ,
610- Class <T > subGraphType ) {
611+ Class <? > subGraphType ) {
611612
612613 if ( isKeySubGraph ) {
613614 return subGraphType != null ? attributeNode .makeKeySubGraph ( subGraphType )
614615 : attributeNode .makeKeySubGraph ();
615616 }
616617
617- return subGraphType != null ?
618- attributeNode .makeSubGraph ( subGraphType ) :
619- attributeNode .makeSubGraph ();
618+ return subGraphType != null
619+ ? attributeNode .makeSubGraph ( subGraphType )
620+ : attributeNode .makeSubGraph ();
620621 }
621622
622623 private <X > Class <X > resolveRequestedClass (String entityName ) {
@@ -783,30 +784,11 @@ public void processJpa(
783784
784785 private void populateStaticMetamodel (MetadataImplementor bootMetamodel , MetadataContext context ) {
785786 bootMetamodel .visitNamedHqlQueryDefinitions ( definition
786- -> injectTypedQueryReference (
787- definition ,
788- namedQueryMetamodelClass (
789- definition ,
790- context
791- )
792- ) );
787+ -> injectTypedQueryReference ( definition , namedQueryMetamodelClass ( definition , context ) ) );
793788 bootMetamodel .visitNamedNativeQueryDefinitions ( definition
794- -> injectTypedQueryReference (
795- definition ,
796- namedQueryMetamodelClass (
797- definition ,
798- context
799- )
800- ) );
789+ -> injectTypedQueryReference ( definition , namedQueryMetamodelClass ( definition , context ) ) );
801790 bootMetamodel .getNamedEntityGraphs ().values ().forEach ( definition
802- -> injectEntityGraph (
803- definition ,
804- graphMetamodelClass (
805- definition ,
806- context
807- ),
808- this
809- ) );
791+ -> injectEntityGraph ( definition , graphMetamodelClass ( definition , context ), this ) );
810792 }
811793
812794 private Class <?> namedQueryMetamodelClass (NamedQueryDefinition <?> definition , MetadataContext context ) {
0 commit comments