HHH-18868 Simplified ID property check

This commit is contained in:
Čedomir Igaly 2024-12-05 12:23:43 +01:00 committed by Marco Belladelli
parent 81a7a20c78
commit 8129ca011a
1 changed files with 19 additions and 12 deletions

View File

@ -16,6 +16,7 @@ import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.boot.spi.MetadataImplementor; import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.mapping.Component; import org.hibernate.mapping.Component;
import org.hibernate.mapping.MappedSuperclass; import org.hibernate.mapping.MappedSuperclass;
@ -55,7 +56,6 @@ import java.util.Set;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import static java.util.Collections.unmodifiableMap; import static java.util.Collections.unmodifiableMap;
import static java.util.stream.Collectors.toSet;
import static org.hibernate.metamodel.internal.InjectionHelper.injectField; import static org.hibernate.metamodel.internal.InjectionHelper.injectField;
/** /**
@ -346,18 +346,22 @@ public class MetadataContext {
final MappedSuperclassDomainType<Object> jpaType = (MappedSuperclassDomainType<Object>) final MappedSuperclassDomainType<Object> jpaType = (MappedSuperclassDomainType<Object>)
mappedSuperclassByMappedSuperclassMapping.get( safeMapping ); mappedSuperclassByMappedSuperclassMapping.get( safeMapping );
final Set<String> idProperties = applyIdMetadata( safeMapping, jpaType ); applyIdMetadata( safeMapping, jpaType );
applyVersionAttribute( safeMapping, jpaType ); applyVersionAttribute( safeMapping, jpaType );
// applyNaturalIdAttribute( safeMapping, jpaType ); // applyNaturalIdAttribute( safeMapping, jpaType );
for ( Property property : safeMapping.getDeclaredProperties() ) { for ( Property property : safeMapping.getDeclaredProperties() ) {
if ( !idProperties.contains( property.getName() ) if ( isIdentifierProperty( property, safeMapping ) ) {
// skip already applied properties // property represents special handling for id-class mappings but we have already
&& (!safeMapping.isVersioned() // accounted for the embedded property mappings in #applyIdMetadata &&
// skip the version property, it was already handled previously. // #buildIdClassAttributes
|| property != safeMapping.getVersion()) ) { continue;
buildAttribute( property, jpaType );
} }
if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
// skip the version property, it was already handled previously.
continue;
}
buildAttribute( property, jpaType );
} }
( (AttributeContainer<?>) jpaType ).getInFlightAccess().finishUp(); ( (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 <T> void addAttribute(EmbeddableDomainType<T> embeddable, Property property, Component component) { private <T> void addAttribute(EmbeddableDomainType<T> embeddable, Property property, Component component) {
final PersistentAttribute<T, ?> attribute = final PersistentAttribute<T, ?> attribute =
attributeFactory.buildAttribute( embeddable, property); attributeFactory.buildAttribute( embeddable, property);
@ -571,7 +581,7 @@ public class MetadataContext {
return embeddableType; return embeddableType;
} }
private <X> Set<String> applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassDomainType<X> jpaMappingType) { private <X> void applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassDomainType<X> jpaMappingType) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final AttributeContainer<X> attributeContainer = (AttributeContainer<X>) jpaMappingType; final AttributeContainer<X> attributeContainer = (AttributeContainer<X>) jpaMappingType;
if ( mappingType.hasIdentifierProperty() ) { if ( mappingType.hasIdentifierProperty() ) {
@ -585,7 +595,6 @@ public class MetadataContext {
attributeFactory::buildIdAttribute attributeFactory::buildIdAttribute
); );
attributeContainer.getInFlightAccess().applyIdAttribute( attribute ); attributeContainer.getInFlightAccess().applyIdAttribute( attribute );
return Set.of(attribute.getName());
} }
} }
//a MappedSuperclass can have no identifier if the id is set below in the hierarchy //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() mappingType.getIdentifierMapper().getProperties()
); );
attributeContainer.getInFlightAccess().applyIdClassAttributes( attributes ); attributeContainer.getInFlightAccess().applyIdClassAttributes( attributes );
return attributes.stream().map( Attribute::getName ).collect( toSet());
} }
return Set.of();
} }
private <X> void applyVersionAttribute(PersistentClass persistentClass, EntityDomainType<X> jpaEntityType) { private <X> void applyVersionAttribute(PersistentClass persistentClass, EntityDomainType<X> jpaEntityType) {