From 8129ca011a19a20f3dccd4514ab980301235b39c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Cedomir=20Igaly?= Date: Thu, 5 Dec 2024 12:23:43 +0100 Subject: [PATCH] HHH-18868 Simplified ID property check --- .../metamodel/internal/MetadataContext.java | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 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 18ebbf2cd9..e3a60f0a0e 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 @@ -16,6 +16,7 @@ import org.hibernate.boot.registry.classloading.spi.ClassLoadingException; import org.hibernate.boot.spi.MetadataImplementor; 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; @@ -55,7 +56,6 @@ import java.util.Set; import java.util.function.BiFunction; import static java.util.Collections.unmodifiableMap; -import static java.util.stream.Collectors.toSet; import static org.hibernate.metamodel.internal.InjectionHelper.injectField; /** @@ -346,18 +346,22 @@ public class MetadataContext { final MappedSuperclassDomainType jpaType = (MappedSuperclassDomainType) mappedSuperclassByMappedSuperclassMapping.get( safeMapping ); - final Set idProperties = applyIdMetadata( safeMapping, jpaType ); + applyIdMetadata( safeMapping, jpaType ); applyVersionAttribute( safeMapping, jpaType ); // applyNaturalIdAttribute( safeMapping, jpaType ); for ( Property property : safeMapping.getDeclaredProperties() ) { - if ( !idProperties.contains( property.getName() ) - // skip already applied properties - && (!safeMapping.isVersioned() - // skip the version property, it was already handled previously. - || property != safeMapping.getVersion()) ) { - buildAttribute( property, jpaType ); + if ( isIdentifierProperty( property, safeMapping ) ) { + // property represents special handling for id-class mappings but we have already + // accounted for the embedded property mappings in #applyIdMetadata && + // #buildIdClassAttributes + continue; } + if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) { + // skip the version property, it was already handled previously. + continue; + } + buildAttribute( property, jpaType ); } ( (AttributeContainer) jpaType ).getInFlightAccess().finishUp(); @@ -409,6 +413,12 @@ public class MetadataContext { } } + private static boolean isIdentifierProperty(Property property, MappedSuperclass mappedSuperclass) { + final Component identifierMapper = mappedSuperclass.getIdentifierMapper(); + return identifierMapper != null ? + ArrayHelper.contains( identifierMapper.getPropertyNames(), property.getName() ) : false; + } + private void addAttribute(EmbeddableDomainType embeddable, Property property, Component component) { final PersistentAttribute attribute = attributeFactory.buildAttribute( embeddable, property); @@ -571,7 +581,7 @@ public class MetadataContext { return embeddableType; } - private Set applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassDomainType jpaMappingType) { + private void applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassDomainType jpaMappingType) { @SuppressWarnings("unchecked") final AttributeContainer attributeContainer = (AttributeContainer) jpaMappingType; if ( mappingType.hasIdentifierProperty() ) { @@ -585,7 +595,6 @@ public class MetadataContext { attributeFactory::buildIdAttribute ); attributeContainer.getInFlightAccess().applyIdAttribute( attribute ); - return Set.of(attribute.getName()); } } //a MappedSuperclass can have no identifier if the id is set below in the hierarchy @@ -596,9 +605,7 @@ public class MetadataContext { mappingType.getIdentifierMapper().getProperties() ); attributeContainer.getInFlightAccess().applyIdClassAttributes( attributes ); - return attributes.stream().map( Attribute::getName ).collect( toSet()); } - return Set.of(); } private void applyVersionAttribute(PersistentClass persistentClass, EntityDomainType jpaEntityType) {