diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/JpaMetamodelImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/JpaMetamodelImpl.java index 5bfaf91dd7..29d1d1532d 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/JpaMetamodelImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/JpaMetamodelImpl.java @@ -7,6 +7,7 @@ package org.hibernate.metamodel.model.domain.internal; import java.io.ObjectStreamException; +import java.io.Serial; import java.io.Serializable; import java.lang.reflect.Field; import java.util.ArrayList; @@ -345,9 +346,8 @@ public class JpaMetamodelImpl implements JpaMetamodelImplementor, Serializable { } } - @Override + @Override @SuppressWarnings("unchecked") public RootGraphImplementor findEntityGraphByName(String name) { - //noinspection unchecked return (RootGraphImplementor) entityGraphMap.get( name ); } @@ -478,7 +478,7 @@ public class JpaMetamodelImpl implements JpaMetamodelImplementor, Serializable { GraphImplementor graphNode) { for ( NamedAttributeNode namedAttributeNode : namedAttributeNodes ) { final String value = namedAttributeNode.value(); - AttributeNodeImplementor attributeNode = graphNode.findOrCreateAttributeNode( value ); + final AttributeNodeImplementor attributeNode = graphNode.findOrCreateAttributeNode( value ); if ( StringHelper.isNotEmpty( namedAttributeNode.subgraph() ) ) { final SubGraphImplementor subgraph = attributeNode.makeSubGraph(); applyNamedSubgraphs( @@ -516,7 +516,8 @@ public class JpaMetamodelImpl implements JpaMetamodelImplementor, Serializable { private Class resolveRequestedClass(String entityName) { try { - return getServiceRegistry().requireService( ClassLoaderService.class ).classForName( entityName ); + return getServiceRegistry().requireService( ClassLoaderService.class ) + .classForName( entityName ); } catch (ClassLoadingException e) { return null; @@ -568,8 +569,9 @@ public class JpaMetamodelImpl implements JpaMetamodelImplementor, Serializable { if ( superType != null && superType.getPersistenceType() == Type.PersistenceType.ENTITY && javaType.isAssignableFrom( superType.getJavaType() ) ) { + final EntityDomainType domainType = (EntityDomainType) superType; final EntityMappingType superMapping = getMappingMetamodel() - .getEntityDescriptor( ( (EntityDomainType) superType ).getHibernateEntityName() ); + .getEntityDescriptor( domainType.getHibernateEntityName() ); if ( !superMapping.isExplicitPolymorphism() ) { continue; } @@ -578,12 +580,10 @@ public class JpaMetamodelImpl implements JpaMetamodelImplementor, Serializable { // it should not be added if it is mapped with explicit polymorphism itself final EntityMappingType entityPersister = getMappingMetamodel() .getEntityDescriptor( managedType.getTypeName() ); - if ( entityPersister.isExplicitPolymorphism() ) { - continue; + if ( !entityPersister.isExplicitPolymorphism() ) { + // aside from these special cases, add it + matchingDescriptors.add( (EntityDomainType) managedType ); } - - // aside from these special cases, add it - matchingDescriptors.add( (EntityDomainType) managedType ); } } @@ -614,7 +614,8 @@ public class JpaMetamodelImpl implements JpaMetamodelImplementor, Serializable { JpaMetaModelPopulationSetting jpaMetaModelPopulationSetting, Collection namedEntityGraphDefinitions, RuntimeModelCreationContext runtimeModelCreationContext) { - bootMetamodel.getImports().forEach( ( k, v ) -> this.nameToImportMap.put( k, new ImportInfo<>( v, null ) ) ); + bootMetamodel.getImports() + .forEach( ( k, v ) -> this.nameToImportMap.put( k, new ImportInfo<>( v, null ) ) ); this.entityProxyInterfaceMap.putAll( entityProxyInterfaceMap ); final MetadataContext context = new MetadataContext( @@ -707,63 +708,49 @@ public class JpaMetamodelImpl implements JpaMetamodelImplementor, Serializable { .add( enumClassName ); } - private EntityDomainType locateOrBuildEntityType( + private EntityDomainType locateOrBuildEntityType( PersistentClass persistentClass, MetadataContext context, - TypeConfiguration typeConfiguration) { - EntityDomainType entityType = context.locateEntityType( persistentClass ); - if ( entityType == null ) { - entityType = buildEntityType( persistentClass, context, typeConfiguration ); - } - return entityType; + final TypeConfiguration typeConfiguration) { + @SuppressWarnings("unchecked") + final EntityDomainType entityType = + (EntityDomainType) + context.locateEntityType( persistentClass ); + return entityType == null + ? buildEntityType( persistentClass, context, typeConfiguration ) + : entityType; } - @SuppressWarnings("unchecked") - private EntityTypeImpl buildEntityType( + private EntityTypeImpl buildEntityType( PersistentClass persistentClass, MetadataContext context, TypeConfiguration typeConfiguration) { context.pushEntityWorkedOn( persistentClass ); - - final MappedSuperclass superMappedSuperclass = persistentClass.getSuperMappedSuperclass(); - - IdentifiableDomainType superType = superMappedSuperclass == null - ? null - : locateOrBuildMappedSuperclassType( superMappedSuperclass, context, typeConfiguration ); - - //no mappedSuperclass, check for a super entity - if ( superType == null ) { - final PersistentClass superPersistentClass = persistentClass.getSuperclass(); - superType = superPersistentClass == null - ? null - : locateOrBuildEntityType( superPersistentClass, context, typeConfiguration ); - } - - final Class javaTypeClass = persistentClass.getMappedClass(); - final JavaType javaType; - if ( javaTypeClass == null || javaTypeClass == Map.class ) { - // dynamic map - javaType = new DynamicModelJavaType(); - } - else { - javaType = context.getTypeConfiguration() - .getJavaTypeRegistry() - .resolveEntityTypeDescriptor( javaTypeClass ); - } - - final EntityTypeImpl entityType = new EntityTypeImpl( - javaType, - superType, - persistentClass, - this - ); - + final EntityTypeImpl entityType = + new EntityTypeImpl<>( + javaType( persistentClass, context ), + supertypeForPersistentClass( persistentClass, context, typeConfiguration ), + persistentClass, + this + ); context.registerEntityType( persistentClass, entityType ); context.popEntityWorkedOn( persistentClass ); - return entityType; } + @SuppressWarnings("unchecked") + private static JavaType javaType(PersistentClass persistentClass, MetadataContext context) { + final Class javaTypeClass = (Class) persistentClass.getMappedClass(); + if ( javaTypeClass == null || Map.class.isAssignableFrom( javaTypeClass ) ) { + // dynamic map + return (JavaType) new DynamicModelJavaType(); + } + else { + return context.getTypeConfiguration().getJavaTypeRegistry() + .resolveEntityTypeDescriptor( javaTypeClass ); + } + } + private void handleUnusedMappedSuperclasses(MetadataContext context, TypeConfiguration typeConfiguration) { final Set unusedMappedSuperclasses = context.getUnusedMappedSuperclasses(); if ( !unusedMappedSuperclasses.isEmpty() ) { @@ -774,50 +761,81 @@ public class JpaMetamodelImpl implements JpaMetamodelImplementor, Serializable { } } - private MappedSuperclassDomainType locateOrBuildMappedSuperclassType( + private MappedSuperclassDomainType locateOrBuildMappedSuperclassType( MappedSuperclass mappedSuperclass, MetadataContext context, TypeConfiguration typeConfiguration) { - MappedSuperclassDomainType mappedSuperclassType = context.locateMappedSuperclassType( mappedSuperclass ); - if ( mappedSuperclassType == null ) { - mappedSuperclassType = buildMappedSuperclassType( mappedSuperclass, context, typeConfiguration ); - } + @SuppressWarnings("unchecked") + final MappedSuperclassDomainType mappedSuperclassType = + (MappedSuperclassDomainType) + context.locateMappedSuperclassType( mappedSuperclass ); + return mappedSuperclassType == null + ? buildMappedSuperclassType( mappedSuperclass, context, typeConfiguration ) + : mappedSuperclassType; + } + + private MappedSuperclassTypeImpl buildMappedSuperclassType( + MappedSuperclass mappedSuperclass, + MetadataContext context, + TypeConfiguration typeConfiguration) { + final IdentifiableDomainType superType = + supertypeForMappedSuperclass( mappedSuperclass, context, typeConfiguration ); + final JavaType javaType = + context.getTypeConfiguration().getJavaTypeRegistry() + .resolveManagedTypeDescriptor( mappedSuperclass.getMappedClass() ); + final MappedSuperclassTypeImpl mappedSuperclassType = + new MappedSuperclassTypeImpl<>( javaType, mappedSuperclass, superType, this ); + context.registerMappedSuperclassType( mappedSuperclass, mappedSuperclassType ); return mappedSuperclassType; } - @SuppressWarnings("unchecked") - private MappedSuperclassTypeImpl buildMappedSuperclassType( + private IdentifiableDomainType supertypeForPersistentClass( + PersistentClass persistentClass, + MetadataContext context, + TypeConfiguration typeConfiguration) { + final MappedSuperclass superMappedSuperclass = persistentClass.getSuperMappedSuperclass(); + final IdentifiableDomainType supertype = + superMappedSuperclass == null + ? null + : locateOrBuildMappedSuperclassType( superMappedSuperclass, context, typeConfiguration ); + + //no mappedSuperclass, check for a super entity + if ( supertype == null ) { + final PersistentClass superPersistentClass = persistentClass.getSuperclass(); + return superPersistentClass == null + ? null + : locateOrBuildEntityType( superPersistentClass, context, typeConfiguration ); + } + else { + return supertype; + } + } + + private IdentifiableDomainType supertypeForMappedSuperclass( MappedSuperclass mappedSuperclass, MetadataContext context, TypeConfiguration typeConfiguration) { final MappedSuperclass superMappedSuperclass = mappedSuperclass.getSuperMappedSuperclass(); - IdentifiableDomainType superType = superMappedSuperclass == null - ? null - : locateOrBuildMappedSuperclassType( superMappedSuperclass, context, typeConfiguration ); + final IdentifiableDomainType superType = + superMappedSuperclass == null + ? null + : locateOrBuildMappedSuperclassType( superMappedSuperclass, context, typeConfiguration ); //no mappedSuperclass, check for a super entity if ( superType == null ) { final PersistentClass superPersistentClass = mappedSuperclass.getSuperPersistentClass(); - superType = superPersistentClass == null + return superPersistentClass == null ? null : locateOrBuildEntityType( superPersistentClass, context, typeConfiguration ); } - final JavaType javaType = context.getTypeConfiguration() - .getJavaTypeRegistry() - .resolveManagedTypeDescriptor( mappedSuperclass.getMappedClass() ); - final MappedSuperclassTypeImpl mappedSuperclassType = new MappedSuperclassTypeImpl( - javaType, - mappedSuperclass, - superType, - this - ); - - context.registerMappedSuperclassType( mappedSuperclass, mappedSuperclassType ); - return mappedSuperclassType; + else { + return superType; + } } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Serialization + @Serial private Object writeReplace() throws ObjectStreamException { return new SerialForm( typeConfiguration.getSessionFactory() ); } @@ -829,6 +847,7 @@ public class JpaMetamodelImpl implements JpaMetamodelImplementor, Serializable { this.sessionFactory = sessionFactory; } + @Serial private Object readResolve() { return sessionFactory.getJpaMetamodel(); } diff --git a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/NamedHqlQueryMementoImpl.java b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/NamedHqlQueryMementoImpl.java index eeeff44238..d0ced0f40c 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/NamedHqlQueryMementoImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/NamedHqlQueryMementoImpl.java @@ -22,8 +22,6 @@ import org.hibernate.query.sqm.internal.SqmSelectionQueryImpl; import org.hibernate.query.sqm.spi.NamedSqmQueryMemento; import org.hibernate.query.sqm.tree.SqmStatement; -import org.jboss.logging.Logger; - import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -36,6 +34,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; */ public class NamedHqlQueryMementoImpl extends AbstractNamedQueryMemento implements NamedSqmQueryMemento, Serializable { + private final String hqlString; private final Integer firstResult;