From de6ea365590e51e062540387acc2e0bca5b3e69d Mon Sep 17 00:00:00 2001 From: Gavin King Date: Wed, 25 Jun 2025 16:31:01 +0200 Subject: [PATCH] some cleanup to code in MetadataContext --- .../metamodel/internal/MetadataContext.java | 190 ++++++++---------- 1 file changed, 80 insertions(+), 110 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java index 09cd1b77e153..d6c80739eeb1 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java @@ -5,7 +5,6 @@ package org.hibernate.metamodel.internal; import jakarta.persistence.metamodel.Attribute; -import jakarta.persistence.metamodel.IdentifiableType; import jakarta.persistence.metamodel.SingularAttribute; import jakarta.persistence.metamodel.Type; import org.hibernate.AssertionFailure; @@ -17,7 +16,6 @@ import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.util.collections.ArrayHelper; -import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.mapping.Component; import org.hibernate.mapping.MappedSuperclass; import org.hibernate.mapping.PersistentClass; @@ -56,6 +54,7 @@ import java.util.function.BiFunction; import static java.util.Collections.unmodifiableMap; +import static org.hibernate.internal.util.collections.CollectionHelper.mapOfSize; import static org.hibernate.metamodel.internal.InjectionHelper.injectField; /** @@ -158,17 +157,11 @@ public Set> getEmbeddableTypeSet() { public Map, MappedSuperclassDomainType> getMappedSuperclassTypeMap() { // we need to actually build this map... - final Map, MappedSuperclassDomainType> mappedSuperClassTypeMap = CollectionHelper.mapOfSize( - mappedSuperclassByMappedSuperclassMapping.size() - ); - - for ( MappedSuperclassDomainType mappedSuperclassType : mappedSuperclassByMappedSuperclassMapping.values() ) { - mappedSuperClassTypeMap.put( - mappedSuperclassType.getJavaType(), - mappedSuperclassType - ); + final Map, MappedSuperclassDomainType> mappedSuperClassTypeMap = + mapOfSize( mappedSuperclassByMappedSuperclassMapping.size() ); + for ( var mappedSuperclassType : mappedSuperclassByMappedSuperclassMapping.values() ) { + mappedSuperClassTypeMap.put( mappedSuperclassType.getJavaType(), mappedSuperclassType ); } - return mappedSuperClassTypeMap; } @@ -263,12 +256,12 @@ public Map> getIdentifiableTypesByName() { Property property, IdentifiableDomainType entityType, BiFunction, Property, PersistentAttribute> factoryFunction) { - final Component component = property.getValue() instanceof Component comp ? comp : null; - if ( component != null && component.isGeneric() ) { + if ( property.getValue() instanceof Component component && component.isGeneric() ) { // This is an embeddable property that uses generics, we have to retrieve the generic // component previously registered and create the concrete attribute - final Component genericComponent = runtimeModelCreationContext.getMetadata() - .getGenericComponent( component.getComponentClass() ); + final Component genericComponent = + runtimeModelCreationContext.getMetadata() + .getGenericComponent( component.getComponentClass() ); final Property genericProperty = property.copy(); genericProperty.setValue( genericComponent ); genericProperty.setGeneric( true ); @@ -382,8 +375,9 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) { while ( ! embeddablesToProcess.isEmpty() ) { - final ArrayList> processingEmbeddables = new ArrayList<>( embeddablesToProcess.size() ); - for ( List> embeddableDomainTypes : embeddablesToProcess.values() ) { + final List> processingEmbeddables = + new ArrayList<>( embeddablesToProcess.size() ); + for ( var embeddableDomainTypes : embeddablesToProcess.values() ) { processingEmbeddables.addAll( embeddableDomainTypes ); } @@ -403,7 +397,6 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) { // generic component embeddables used just for concrete type resolution if ( !component.isGeneric() && !( embeddable.getExpressibleJavaType() instanceof EntityJavaType ) ) { embeddables.put( embeddable.getJavaType(), embeddable ); - if ( staticMetamodelScanEnabled ) { populateStaticMetamodel( embeddable, processedMetamodelClasses ); } @@ -414,27 +407,24 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) { private static boolean isIdentifierProperty(Property property, MappedSuperclass mappedSuperclass) { final Component identifierMapper = mappedSuperclass.getIdentifierMapper(); - return identifierMapper != null && ArrayHelper.contains( - identifierMapper.getPropertyNames(), - property.getName() - ); + return identifierMapper != null + && ArrayHelper.contains( identifierMapper.getPropertyNames(), property.getName() ); } private void addAttribute(EmbeddableDomainType embeddable, Property property, Component component) { final PersistentAttribute attribute = attributeFactory.buildAttribute( embeddable, property); if ( attribute != null ) { - final Property superclassProperty = getMappedSuperclassProperty( - property.getName(), - component.getMappedSuperclass() - ); + final Property superclassProperty = + getMappedSuperclassProperty( property.getName(), + component.getMappedSuperclass() ); if ( superclassProperty != null && superclassProperty.isGeneric() ) { @SuppressWarnings("unchecked") - final AttributeContainer attributeContainer = (AttributeContainer) embeddable; + final var attributeContainer = (AttributeContainer) embeddable; attributeContainer.getInFlightAccess().addConcreteGenericAttribute( attribute ); } else { - addAttribute(embeddable, attribute ); + addAttribute( embeddable, attribute ); } } } @@ -456,8 +446,9 @@ private void addAttribute(ManagedDomainType type, PersistentAttribute container = (AttributeContainer) type; final AttributeContainer.InFlightAccess inFlightAccess = container.getInFlightAccess(); - final boolean virtual = attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED - && attribute.getAttributeJavaType() instanceof EntityJavaType; + final boolean virtual = + attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED + && attribute.getAttributeJavaType() instanceof EntityJavaType; if ( virtual ) { @SuppressWarnings("unchecked") final EmbeddableDomainType embeddableDomainType = @@ -487,24 +478,19 @@ private void applyIdMetadata(PersistentClass persistentClass, IdentifiableDo @SuppressWarnings("unchecked") final AttributeContainer attributeContainer = (AttributeContainer) identifiableType; if ( declaredIdentifierProperty != null ) { - final SingularPersistentAttribute idAttribute = + final var idAttribute = (SingularPersistentAttribute) - buildAttribute( - declaredIdentifierProperty, - identifiableType, - attributeFactory::buildIdAttribute - ); + buildAttribute( declaredIdentifierProperty, identifiableType, + attributeFactory::buildIdAttribute ); attributeContainer.getInFlightAccess().applyIdAttribute( idAttribute ); } else { final Property superclassIdentifier = getMappedSuperclassIdentifier( persistentClass ); if ( superclassIdentifier != null && superclassIdentifier.isGeneric() ) { // If the superclass identifier is generic we have to build the attribute to register the concrete type - final SingularPersistentAttribute concreteIdentifier = - attributeFactory.buildIdAttribute( - identifiableType, - persistentClass.getIdentifierProperty() - ); + final var concreteIdentifier = + attributeFactory.buildIdAttribute( identifiableType, + persistentClass.getIdentifierProperty() ); attributeContainer.getInFlightAccess().addConcreteGenericAttribute( concreteIdentifier ); } } @@ -542,7 +528,7 @@ private void applyIdMetadata(PersistentClass persistentClass, IdentifiableDo final IdentifiableDomainType idDomainType = identifiableTypesByName.get( compositeId.getOwner().getEntityName() ); @SuppressWarnings("unchecked") - final AbstractIdentifiableType idType = (AbstractIdentifiableType) idDomainType; + final var idType = (AbstractIdentifiableType) idDomainType; applyIdAttributes( identifiableType, idType, propertySpan, cidProperties, idClassType ); } } @@ -562,7 +548,7 @@ private void applyIdAttributes( } @SuppressWarnings("unchecked") - final AttributeContainer container = (AttributeContainer) identifiableType; + final var container = (AttributeContainer) identifiableType; container.getInFlightAccess().applyNonAggregatedIdAttributes( idAttributes, idClassType); } @@ -579,54 +565,45 @@ private Property getMappedSuperclassIdentifier(PersistentClass persistentClass) } private EmbeddableTypeImpl applyIdClassMetadata(Component idClassComponent) { - final JavaType javaType = - getTypeConfiguration().getJavaTypeRegistry() - .resolveManagedTypeDescriptor( idClassComponent.getComponentClass() ); - - final MappedSuperclass mappedSuperclass = idClassComponent.getMappedSuperclass(); - final MappedSuperclassDomainType superType; - if ( mappedSuperclass != null ) { - //noinspection unchecked - superType = (MappedSuperclassDomainType) locateMappedSuperclassType( mappedSuperclass ); - } - else { - superType = null; - } - - final EmbeddableTypeImpl embeddableType = new EmbeddableTypeImpl<>( - javaType, - superType, - null, - false, - getJpaMetamodel() - ); + final EmbeddableTypeImpl embeddableType = + new EmbeddableTypeImpl<>( + getTypeConfiguration().getJavaTypeRegistry() + .resolveManagedTypeDescriptor( idClassComponent.getComponentClass() ), + getMappedSuperclassDomainType( idClassComponent ), + null, + false, + getJpaMetamodel() + ); registerEmbeddableType( embeddableType, idClassComponent ); return embeddableType; } + @SuppressWarnings("unchecked") + private MappedSuperclassDomainType getMappedSuperclassDomainType(Component idClassComponent) { + final MappedSuperclass mappedSuperclass = idClassComponent.getMappedSuperclass(); + return mappedSuperclass == null ? null + : (MappedSuperclassDomainType) + locateMappedSuperclassType( mappedSuperclass ); + } + private void applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassDomainType jpaMappingType) { @SuppressWarnings("unchecked") - final AttributeContainer attributeContainer = (AttributeContainer) jpaMappingType; + final var attributeContainer = (AttributeContainer) jpaMappingType; if ( mappingType.hasIdentifierProperty() ) { final Property declaredIdentifierProperty = mappingType.getDeclaredIdentifierProperty(); if ( declaredIdentifierProperty != null ) { - final SingularPersistentAttribute attribute = + final var attribute = (SingularPersistentAttribute) - buildAttribute( - declaredIdentifierProperty, - jpaMappingType, - attributeFactory::buildIdAttribute - ); + buildAttribute( declaredIdentifierProperty, jpaMappingType, + attributeFactory::buildIdAttribute ); attributeContainer.getInFlightAccess().applyIdAttribute( attribute ); } } //a MappedSuperclass can have no identifier if the id is set below in the hierarchy else if ( mappingType.getIdentifierMapper() != null ) { - final Set> attributes = - buildIdClassAttributes( - jpaMappingType, - mappingType.getIdentifierMapper().getProperties() - ); + final var attributes = + buildIdClassAttributes( jpaMappingType, + mappingType.getIdentifierMapper().getProperties() ); attributeContainer.getInFlightAccess().applyIdClassAttributes( attributes ); } } @@ -635,10 +612,9 @@ private void applyVersionAttribute(PersistentClass persistentClass, EntityDo final Property declaredVersion = persistentClass.getDeclaredVersion(); if ( declaredVersion != null ) { @SuppressWarnings("unchecked") - final AttributeContainer attributeContainer = (AttributeContainer) jpaEntityType; - attributeContainer.getInFlightAccess().applyVersionAttribute( - attributeFactory.buildVersionAttribute( jpaEntityType, declaredVersion ) - ); + final var attributeContainer = (AttributeContainer) jpaEntityType; + attributeContainer.getInFlightAccess() + .applyVersionAttribute( attributeFactory.buildVersionAttribute( jpaEntityType, declaredVersion ) ); } } @@ -646,10 +622,9 @@ private void applyVersionAttribute(MappedSuperclass mappingType, MappedSuper final Property declaredVersion = mappingType.getDeclaredVersion(); if ( declaredVersion != null ) { @SuppressWarnings("unchecked") - final AttributeContainer xAttributeContainer = (AttributeContainer) jpaMappingType; - xAttributeContainer.getInFlightAccess().applyVersionAttribute( - attributeFactory.buildVersionAttribute( jpaMappingType, declaredVersion ) - ); + final var attributeContainer = (AttributeContainer) jpaMappingType; + attributeContainer.getInFlightAccess() + .applyVersionAttribute( attributeFactory.buildVersionAttribute( jpaMappingType, declaredVersion ) ); } } @@ -661,7 +636,7 @@ private void applyGenericProperties(PersistentClass persistentClass, EntityD final Property property = persistentClass.getProperty( superclassProperty.getName() ); final PersistentAttribute attribute = attributeFactory.buildAttribute( entityType, property ); @SuppressWarnings("unchecked") - final AttributeContainer attributeContainer = (AttributeContainer) entityType; + final var attributeContainer = (AttributeContainer) entityType; attributeContainer.getInFlightAccess().addConcreteGenericAttribute( attribute ); } } @@ -682,9 +657,9 @@ private MappedSuperclass getMappedSuperclass(PersistentClass persistentClass) { private MappedSuperclass getMappedSuperclass(MappedSuperclass mappedSuperclass) { final MappedSuperclass superMappedSuperclass = mappedSuperclass.getSuperMappedSuperclass(); - return superMappedSuperclass != null - ? superMappedSuperclass - : getMappedSuperclass( mappedSuperclass.getSuperPersistentClass() ); + return superMappedSuperclass == null + ? getMappedSuperclass( mappedSuperclass.getSuperPersistentClass() ) + : superMappedSuperclass; } private Property getMappedSuperclassProperty(String propertyName, MappedSuperclass mappedSuperclass) { @@ -731,7 +706,8 @@ private void populateStaticMetamodel(ManagedDomainType managedType, Set metamodelClass = metamodelClass( managedType ); if ( metamodelClass != null ) { - populateMetamodelClass( managedType, metamodelClass ); + registerAttributes( metamodelClass, managedType ); + injectManagedType( managedType, metamodelClass ); } // todo : this does not account for @MappedSuperclass, mainly // because this is not being tracked in our internal @@ -743,11 +719,6 @@ private void populateStaticMetamodel(ManagedDomainType managedType, Set void populateMetamodelClass(ManagedDomainType managedType, Class metamodelClass) { - registerAttributes( metamodelClass, managedType ); - injectManagedType( managedType, metamodelClass ); - } - private static void injectManagedType(ManagedDomainType managedType, Class metamodelClass) { try { injectField( metamodelClass, "class_", managedType, false ); @@ -788,9 +759,7 @@ private void registerAttributes(Class metamodelClass, ManagedDomainType entityType = (AbstractIdentifiableType) managedType; - + if ( managedType instanceof AbstractIdentifiableType entityType ) { // handle version if ( entityType.hasDeclaredVersionAttribute() ) { registerAttribute( metamodelClass, entityType.getDeclaredVersion() ); @@ -798,7 +767,7 @@ private void registerAttributes(Class metamodelClass, ManagedDomainType> attributes = entityType.getIdClassAttributesSafely(); + final var attributes = entityType.getIdClassAttributesSafely(); if ( attributes != null ) { for ( SingularAttribute attribute : attributes ) { registerAttribute( metamodelClass, attribute ); @@ -833,7 +802,6 @@ private void registerAttribute(Class metamodelClass, Attribute attr } } - public MappedSuperclassDomainType locateMappedSuperclassType(MappedSuperclass mappedSuperclass) { return mappedSuperclassByMappedSuperclassMapping.get( mappedSuperclass ); } @@ -874,22 +842,24 @@ public BasicDomainType resolveBasicType(Class javaType) { if ( domainType == null ) { // we cannot use getTypeConfiguration().standardBasicTypeForJavaType(javaType) // because that doesn't return the right thing for primitive types - final JavaType javaTypeDescriptor = - getTypeConfiguration().getJavaTypeRegistry().resolveDescriptor( javaType ); - final JdbcType jdbcType = - javaTypeDescriptor.getRecommendedJdbcType( typeConfiguration.getCurrentBaseSqlTypeIndicators() ); - final BasicDomainType type = - javaType.isPrimitive() - ? new PrimitiveBasicTypeImpl<>( javaTypeDescriptor, jdbcType, javaType ) - : new BasicTypeImpl<>( javaTypeDescriptor, jdbcType ); - basicDomainTypeMap.put( javaType, type ); - return type; + basicDomainTypeMap.put( javaType, basicDomainType( javaType ) ); + return basicDomainType( javaType ); } else { return domainType; } } + private BasicDomainType basicDomainType(Class javaType) { + final JavaType javaTypeDescriptor = + getTypeConfiguration().getJavaTypeRegistry().resolveDescriptor( javaType ); + final JdbcType jdbcType = + javaTypeDescriptor.getRecommendedJdbcType( typeConfiguration.getCurrentBaseSqlTypeIndicators() ); + return javaType.isPrimitive() + ? new PrimitiveBasicTypeImpl<>( javaTypeDescriptor, jdbcType, javaType ) + : new BasicTypeImpl<>( javaTypeDescriptor, jdbcType ); + } + public EmbeddableDomainType locateEmbeddable(Class embeddableClass, Component component) { //noinspection unchecked EmbeddableDomainType domainType = (EmbeddableDomainType) embeddables.get( embeddableClass );