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,19 +514,25 @@ 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-
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 was a subtype of the graph type: '"
531+ + subgraphType .getName () + "' is not a subtype of '" + 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 );
529536 applyNamedAttributeNodes (
530537 subclassSubgraph .attributeNodes (),
531538 namedEntityGraph ,
@@ -541,7 +548,7 @@ private <T> RootGraphImpl<T> createEntityGraph(
541548 return entityGraph ;
542549 }
543550
544- private static <T > RootGraphImpl <T > createRootGraph (
551+ private static <T > RootGraphImplementor <T > createRootGraph (
545552 String name , EntityDomainType <T > entityType , boolean includeAllAttributes ) {
546553 final RootGraphImpl <T > entityGraph = new RootGraphImpl <>( name , entityType );
547554 if ( includeAllAttributes ) {
@@ -783,30 +790,11 @@ public void processJpa(
783790
784791 private void populateStaticMetamodel (MetadataImplementor bootMetamodel , MetadataContext context ) {
785792 bootMetamodel .visitNamedHqlQueryDefinitions ( definition
786- -> injectTypedQueryReference (
787- definition ,
788- namedQueryMetamodelClass (
789- definition ,
790- context
791- )
792- ) );
793+ -> injectTypedQueryReference ( definition , namedQueryMetamodelClass ( definition , context ) ) );
793794 bootMetamodel .visitNamedNativeQueryDefinitions ( definition
794- -> injectTypedQueryReference (
795- definition ,
796- namedQueryMetamodelClass (
797- definition ,
798- context
799- )
800- ) );
795+ -> injectTypedQueryReference ( definition , namedQueryMetamodelClass ( definition , context ) ) );
801796 bootMetamodel .getNamedEntityGraphs ().values ().forEach ( definition
802- -> injectEntityGraph (
803- definition ,
804- graphMetamodelClass (
805- definition ,
806- context
807- ),
808- this
809- ) );
797+ -> injectEntityGraph ( definition , graphMetamodelClass ( definition , context ), this ) );
810798 }
811799
812800 private Class <?> namedQueryMetamodelClass (NamedQueryDefinition <?> definition , MetadataContext context ) {
0 commit comments