Skip to content

Commit 8129ca0

Browse files
cigalymbellade
authored andcommitted
HHH-18868 Simplified ID property check
1 parent 81a7a20 commit 8129ca0

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.hibernate.boot.spi.MetadataImplementor;
1717
import org.hibernate.internal.CoreLogging;
1818
import org.hibernate.internal.CoreMessageLogger;
19+
import org.hibernate.internal.util.collections.ArrayHelper;
1920
import org.hibernate.internal.util.collections.CollectionHelper;
2021
import org.hibernate.mapping.Component;
2122
import org.hibernate.mapping.MappedSuperclass;
@@ -55,7 +56,6 @@
5556
import java.util.function.BiFunction;
5657

5758
import static java.util.Collections.unmodifiableMap;
58-
import static java.util.stream.Collectors.toSet;
5959
import static org.hibernate.metamodel.internal.InjectionHelper.injectField;
6060

6161
/**
@@ -346,18 +346,22 @@ else if ( MappedSuperclass.class.isAssignableFrom( mapping.getClass() ) ) {
346346
final MappedSuperclassDomainType<Object> jpaType = (MappedSuperclassDomainType<Object>)
347347
mappedSuperclassByMappedSuperclassMapping.get( safeMapping );
348348

349-
final Set<String> idProperties = applyIdMetadata( safeMapping, jpaType );
349+
applyIdMetadata( safeMapping, jpaType );
350350
applyVersionAttribute( safeMapping, jpaType );
351351
// applyNaturalIdAttribute( safeMapping, jpaType );
352352

353353
for ( Property property : safeMapping.getDeclaredProperties() ) {
354-
if ( !idProperties.contains( property.getName() )
355-
// skip already applied properties
356-
&& (!safeMapping.isVersioned()
357-
// skip the version property, it was already handled previously.
358-
|| property != safeMapping.getVersion()) ) {
359-
buildAttribute( property, jpaType );
354+
if ( isIdentifierProperty( property, safeMapping ) ) {
355+
// property represents special handling for id-class mappings but we have already
356+
// accounted for the embedded property mappings in #applyIdMetadata &&
357+
// #buildIdClassAttributes
358+
continue;
359+
}
360+
if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
361+
// skip the version property, it was already handled previously.
362+
continue;
360363
}
364+
buildAttribute( property, jpaType );
361365
}
362366

363367
( (AttributeContainer<?>) jpaType ).getInFlightAccess().finishUp();
@@ -409,6 +413,12 @@ else if ( MappedSuperclass.class.isAssignableFrom( mapping.getClass() ) ) {
409413
}
410414
}
411415

416+
private static boolean isIdentifierProperty(Property property, MappedSuperclass mappedSuperclass) {
417+
final Component identifierMapper = mappedSuperclass.getIdentifierMapper();
418+
return identifierMapper != null ?
419+
ArrayHelper.contains( identifierMapper.getPropertyNames(), property.getName() ) : false;
420+
}
421+
412422
private <T> void addAttribute(EmbeddableDomainType<T> embeddable, Property property, Component component) {
413423
final PersistentAttribute<T, ?> attribute =
414424
attributeFactory.buildAttribute( embeddable, property);
@@ -571,7 +581,7 @@ private EmbeddableTypeImpl<?> applyIdClassMetadata(Component idClassComponent) {
571581
return embeddableType;
572582
}
573583

574-
private <X> Set<String> applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassDomainType<X> jpaMappingType) {
584+
private <X> void applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassDomainType<X> jpaMappingType) {
575585
@SuppressWarnings("unchecked")
576586
final AttributeContainer<X> attributeContainer = (AttributeContainer<X>) jpaMappingType;
577587
if ( mappingType.hasIdentifierProperty() ) {
@@ -585,7 +595,6 @@ private <X> Set<String> applyIdMetadata(MappedSuperclass mappingType, MappedSupe
585595
attributeFactory::buildIdAttribute
586596
);
587597
attributeContainer.getInFlightAccess().applyIdAttribute( attribute );
588-
return Set.of(attribute.getName());
589598
}
590599
}
591600
//a MappedSuperclass can have no identifier if the id is set below in the hierarchy
@@ -596,9 +605,7 @@ else if ( mappingType.getIdentifierMapper() != null ) {
596605
mappingType.getIdentifierMapper().getProperties()
597606
);
598607
attributeContainer.getInFlightAccess().applyIdClassAttributes( attributes );
599-
return attributes.stream().map( Attribute::getName ).collect( toSet());
600608
}
601-
return Set.of();
602609
}
603610

604611
private <X> void applyVersionAttribute(PersistentClass persistentClass, EntityDomainType<X> jpaEntityType) {

0 commit comments

Comments
 (0)