From 3a8e66d6004bc45bd12e564dd5f8ea7c18fd61db Mon Sep 17 00:00:00 2001 From: Gavin Date: Mon, 12 Jun 2023 22:39:12 +0200 Subject: [PATCH] HHH-16783 split @Any discriminator handling into two subclasses --- .../metamodel/internal/AttributeFactory.java | 10 +- .../DefaultDiscriminatorConverter.java | 90 ++++++++++++ .../mapping/DiscriminatorConverter.java | 131 ++--------------- .../mapping/MappedDiscriminatorConverter.java | 134 ++++++++++++++++++ .../internal/AnyDiscriminatorPart.java | 27 ++-- .../DiscriminatedAssociationMapping.java | 10 +- .../internal/AnyMappingDomainTypeImpl.java | 31 ++-- .../entity/AbstractEntityPersister.java | 5 +- 8 files changed, 285 insertions(+), 153 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/metamodel/mapping/DefaultDiscriminatorConverter.java create mode 100644 hibernate-core/src/main/java/org/hibernate/metamodel/mapping/MappedDiscriminatorConverter.java diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/AttributeFactory.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/AttributeFactory.java index 1931bdfdb9..ad4fbe8dfa 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/AttributeFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/AttributeFactory.java @@ -252,7 +252,8 @@ public class AttributeFactory { final org.hibernate.type.Type type = typeContext.getHibernateValue().getType(); if ( type instanceof EntityType ) { final EntityType entityType = (EntityType) type; - final IdentifiableDomainType domainType = context.locateIdentifiableType( entityType.getAssociatedEntityName() ); + final IdentifiableDomainType domainType = + context.locateIdentifiableType( entityType.getAssociatedEntityName() ); if ( domainType == null ) { // Due to the use of generics, it can happen that a mapped super class uses a type // for an attribute that is not a managed type. Since this case is not specifically mentioned @@ -274,9 +275,7 @@ public class AttributeFactory { (Any) typeContext.getHibernateValue(), anyType, (JavaType) baseJtd, - context.getTypeConfiguration(), - context.getMetamodel(), - context.getRuntimeModelCreationContext().getSessionFactory() + context.getRuntimeModelCreationContext().getSessionFactory().getMappingMetamodel() ); } case EMBEDDABLE: { @@ -344,7 +343,8 @@ public class AttributeFactory { DomainType metaModelType, MetadataContext context) { if ( typeContext.getValueClassification() == ValueClassification.BASIC ) { - final ConverterDescriptor descriptor = ( (SimpleValue) typeContext.getHibernateValue() ).getJpaAttributeConverterDescriptor(); + final ConverterDescriptor descriptor = + ( (SimpleValue) typeContext.getHibernateValue() ).getJpaAttributeConverterDescriptor(); if ( descriptor != null ) { return context.getJavaTypeRegistry().resolveDescriptor( descriptor.getRelationalValueResolvedType().getErasedType() diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/DefaultDiscriminatorConverter.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/DefaultDiscriminatorConverter.java new file mode 100644 index 0000000000..3364d8c154 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/DefaultDiscriminatorConverter.java @@ -0,0 +1,90 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html. + */ +package org.hibernate.metamodel.mapping; + +import org.hibernate.AssertionFailure; +import org.hibernate.HibernateException; +import org.hibernate.metamodel.mapping.internal.DiscriminatorValueDetailsImpl; +import org.hibernate.metamodel.model.domain.NavigableRole; +import org.hibernate.metamodel.spi.MappingMetamodelImplementor; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.type.BasicType; +import org.hibernate.type.descriptor.java.JavaType; + +import java.util.function.Consumer; +import java.util.function.Function; + +/** + * Implements the default discriminator assignment strategy defined by JPA, + * that is, the discriminator value is the JPA (unqualified) entity name. + * This strategy is used when no explicit discriminator mapping is specified. + * + * @author Gavin King + */ +public class DefaultDiscriminatorConverter extends DiscriminatorConverter { + + public static DefaultDiscriminatorConverter fromMappingMetamodel( + NavigableRole role, + JavaType domainJavaType, + BasicType underlyingJdbcMapping, + MappingMetamodelImplementor mappingMetamodel) { + return new DefaultDiscriminatorConverter<>( + role, + domainJavaType, + underlyingJdbcMapping.getJavaTypeDescriptor(), + mappingMetamodel + ); + } + + private final MappingMetamodelImplementor mappingMetamodel; + + public DefaultDiscriminatorConverter( + NavigableRole discriminatorRole, + JavaType domainJavaType, + JavaType relationalJavaType, + MappingMetamodelImplementor mappingMetamodel) { + super( discriminatorRole, domainJavaType, relationalJavaType ); + this.mappingMetamodel = mappingMetamodel; + } + + @Override + public DiscriminatorValueDetails getDetailsForRelationalForm(R relationalForm) { + return getDetailsForDiscriminatorValue( relationalForm ); + } + + @Override + public DiscriminatorValueDetails getDetailsForEntityName(String entityName) { + EntityPersister persister = mappingMetamodel.findEntityDescriptor( entityName ); + if ( persister!= null ) { + return new DiscriminatorValueDetailsImpl( entityName, persister ); + } + + throw new AssertionFailure( "Unrecognized entity name: " + entityName ); + } + + @Override + public DiscriminatorValueDetails getDetailsForDiscriminatorValue(Object value) { + if ( value instanceof String ) { + String entityName = mappingMetamodel.getImportedName( (String) value ); + EntityPersister persister = mappingMetamodel.findEntityDescriptor( entityName ); + if ( persister!= null ) { + return new DiscriminatorValueDetailsImpl( entityName, persister ); + } + } + + throw new HibernateException( "Unrecognized discriminator value: " + value ); + } + + @Override + public void forEachValueDetail(Consumer consumer) { + } + + @Override + public X fromValueDetails(Function handler) { + return null; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/DiscriminatorConverter.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/DiscriminatorConverter.java index d25290ee63..0e036b9f5a 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/DiscriminatorConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/DiscriminatorConverter.java @@ -6,94 +6,31 @@ */ package org.hibernate.metamodel.mapping; -import java.util.List; -import java.util.Map; -import java.util.function.Consumer; -import java.util.function.Function; - -import org.hibernate.AssertionFailure; -import org.hibernate.HibernateException; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.metamodel.RepresentationMode; -import org.hibernate.metamodel.mapping.internal.DiscriminatorValueDetailsImpl; import org.hibernate.metamodel.model.domain.NavigableRole; -import org.hibernate.metamodel.spi.MappingMetamodelImplementor; -import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.type.BasicType; import org.hibernate.type.descriptor.converter.spi.BasicValueConverter; import org.hibernate.type.descriptor.java.JavaType; -import static org.hibernate.persister.entity.DiscriminatorHelper.NOT_NULL_DISCRIMINATOR; -import static org.hibernate.persister.entity.DiscriminatorHelper.NULL_DISCRIMINATOR; +import java.util.function.Consumer; +import java.util.function.Function; /** - * Conversion of discriminator values between the entity name/Class domain form and - * its generally CHARACTER or INTEGER based relational form - * - * @param The domain type - either
    - *
  • - * the {@linkplain EntityMappingType#getMappedJavaType() entity Class} for unnamed entities - *
  • - *
  • - * the {@linkplain EntityMappingType#getEntityName() entity name} for named entities - *
  • - *
- * @param The Java type of the relational form of the discriminator - * * @author Steve Ebersole + * @author Gavin King */ -public class DiscriminatorConverter implements BasicValueConverter { - public static DiscriminatorConverter fromValueMappings( - NavigableRole role, - JavaType domainJavaType, - BasicType underlyingJdbcMapping, - Map valueMappings, - SessionFactoryImplementor sessionFactory) { - final MappingMetamodelImplementor mappingMetamodel = sessionFactory.getMappingMetamodel(); - final List valueDetailsList = CollectionHelper.arrayList( valueMappings.size() ); - valueMappings.forEach( (value, entityName) -> { - final DiscriminatorValueDetails valueDetails = new DiscriminatorValueDetailsImpl( - value, - mappingMetamodel.getEntityDescriptor( entityName ) - ); - valueDetailsList.add( valueDetails ); - } ); - - return new DiscriminatorConverter<>( - role, - domainJavaType, - underlyingJdbcMapping.getJavaTypeDescriptor(), - valueDetailsList, - mappingMetamodel - ); - } +public abstract class DiscriminatorConverter implements BasicValueConverter { private final NavigableRole discriminatorRole; private final JavaType domainJavaType; private final JavaType relationalJavaType; - private final Map discriminatorValueToEntityNameMap; - private final Map entityNameToDiscriminatorValueMap; - private final MappingMetamodelImplementor mappingMetamodel; - public DiscriminatorConverter( NavigableRole discriminatorRole, JavaType domainJavaType, - JavaType relationalJavaType, - List valueMappings, - MappingMetamodelImplementor mappingMetamodel) { + JavaType relationalJavaType) { this.discriminatorRole = discriminatorRole; this.domainJavaType = domainJavaType; this.relationalJavaType = relationalJavaType; - this.mappingMetamodel = mappingMetamodel; - - this.discriminatorValueToEntityNameMap = CollectionHelper.concurrentMap( valueMappings.size() ); - this.entityNameToDiscriminatorValueMap = CollectionHelper.concurrentMap( valueMappings.size() ); - valueMappings.forEach( (valueDetails) -> { - discriminatorValueToEntityNameMap.put( valueDetails.getValue(), valueDetails ); - entityNameToDiscriminatorValueMap.put( valueDetails.getIndicatedEntityName(), valueDetails ); - } ); } public NavigableRole getNavigableRole() { @@ -131,46 +68,6 @@ public class DiscriminatorConverter implements BasicValueConverter { : (O) indicatedEntity.getEntityName(); } - public DiscriminatorValueDetails getDetailsForEntityName(String entityName) { - DiscriminatorValueDetails valueDetails = entityNameToDiscriminatorValueMap.get( entityName ); - if ( valueDetails!= null) { - return valueDetails; - } - - EntityPersister persister = mappingMetamodel.findEntityDescriptor( entityName ); - if ( persister!= null ) { - return new DiscriminatorValueDetailsImpl( entityName, persister ); - } - - throw new AssertionFailure( "Unrecognized entity name: " + entityName ); - } - - public DiscriminatorValueDetails getDetailsForDiscriminatorValue(Object value) { - if ( value == null ) { - return discriminatorValueToEntityNameMap.get( NULL_DISCRIMINATOR ); - } - - final DiscriminatorValueDetails valueMatch = discriminatorValueToEntityNameMap.get( value ); - if ( valueMatch != null ) { - return valueMatch; - } - - final DiscriminatorValueDetails notNullMatch = discriminatorValueToEntityNameMap.get( NOT_NULL_DISCRIMINATOR ); - if ( notNullMatch != null ) { - return notNullMatch; - } - - if ( value instanceof String ) { - String entityName = mappingMetamodel.getImportedName( (String) value ); - EntityPersister persister = mappingMetamodel.findEntityDescriptor( entityName ); - if ( persister!= null ) { - return new DiscriminatorValueDetailsImpl( entityName, persister ); - } - } - - throw new HibernateException( "Unrecognized discriminator value: " + value ); - } - @Override public R toRelationalValue(O domainForm) { assert domainForm == null || domainForm instanceof String || domainForm instanceof Class; @@ -192,22 +89,16 @@ public class DiscriminatorConverter implements BasicValueConverter { return (R) discriminatorValueDetails.getValue(); } + public abstract DiscriminatorValueDetails getDetailsForDiscriminatorValue(Object relationalForm); + + public abstract DiscriminatorValueDetails getDetailsForEntityName(String entityName); + @Override public String toString() { return "DiscriminatorConverter(" + discriminatorRole.getFullPath() + ")"; } - public void forEachValueDetail(Consumer consumer) { - discriminatorValueToEntityNameMap.forEach( (value, detail) -> consumer.accept( detail ) ); - } + public abstract void forEachValueDetail(Consumer consumer); - public X fromValueDetails(Function handler) { - for ( DiscriminatorValueDetails detail : discriminatorValueToEntityNameMap.values() ) { - final X result = handler.apply( detail ); - if ( result != null ) { - return result; - } - } - return null; - } + public abstract X fromValueDetails(Function handler); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/MappedDiscriminatorConverter.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/MappedDiscriminatorConverter.java new file mode 100644 index 0000000000..8c1a66c113 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/MappedDiscriminatorConverter.java @@ -0,0 +1,134 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html. + */ +package org.hibernate.metamodel.mapping; + +import org.hibernate.AssertionFailure; +import org.hibernate.HibernateException; +import org.hibernate.internal.util.collections.CollectionHelper; +import org.hibernate.metamodel.mapping.internal.DiscriminatorValueDetailsImpl; +import org.hibernate.metamodel.model.domain.NavigableRole; +import org.hibernate.metamodel.spi.MappingMetamodelImplementor; +import org.hibernate.type.BasicType; +import org.hibernate.type.descriptor.java.JavaType; + +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Function; + +import static org.hibernate.persister.entity.DiscriminatorHelper.NOT_NULL_DISCRIMINATOR; +import static org.hibernate.persister.entity.DiscriminatorHelper.NULL_DISCRIMINATOR; + +/** + * Conversion of discriminator values between the entity name/Class domain form and + * its generally CHARACTER or INTEGER based relational form + * + * @param The domain type - either
    + *
  • + * the {@linkplain EntityMappingType#getMappedJavaType() entity Class} for unnamed entities + *
  • + *
  • + * the {@linkplain EntityMappingType#getEntityName() entity name} for named entities + *
  • + *
+ * @param The Java type of the relational form of the discriminator + * + * @author Steve Ebersole + */ +public class MappedDiscriminatorConverter extends DiscriminatorConverter { + + public static MappedDiscriminatorConverter fromValueMappings( + NavigableRole role, + JavaType domainJavaType, + BasicType underlyingJdbcMapping, + Map valueMappings, + MappingMetamodelImplementor mappingMetamodel) { + final List valueDetailsList = CollectionHelper.arrayList( valueMappings.size() ); + valueMappings.forEach( (value, entityName) -> { + final DiscriminatorValueDetails valueDetails = new DiscriminatorValueDetailsImpl( + value, + mappingMetamodel.getEntityDescriptor( entityName ) + ); + valueDetailsList.add( valueDetails ); + } ); + + return new MappedDiscriminatorConverter<>( + role, + domainJavaType, + underlyingJdbcMapping.getJavaTypeDescriptor(), + valueDetailsList + ); + } + + private final Map discriminatorValueToEntityNameMap; + private final Map entityNameToDiscriminatorValueMap; + + public MappedDiscriminatorConverter( + NavigableRole discriminatorRole, + JavaType domainJavaType, + JavaType relationalJavaType, + List valueMappings) { + super( discriminatorRole, domainJavaType, relationalJavaType ); + + this.discriminatorValueToEntityNameMap = CollectionHelper.concurrentMap( valueMappings.size() ); + this.entityNameToDiscriminatorValueMap = CollectionHelper.concurrentMap( valueMappings.size() ); + valueMappings.forEach( (valueDetails) -> { + discriminatorValueToEntityNameMap.put( valueDetails.getValue(), valueDetails ); + entityNameToDiscriminatorValueMap.put( valueDetails.getIndicatedEntityName(), valueDetails ); + } ); + } + + @Override + public DiscriminatorValueDetails getDetailsForRelationalForm(R relationalForm) { + return getDetailsForDiscriminatorValue( relationalForm ); + } + + @Override + public DiscriminatorValueDetails getDetailsForEntityName(String entityName) { + DiscriminatorValueDetails valueDetails = entityNameToDiscriminatorValueMap.get( entityName ); + if ( valueDetails!= null) { + return valueDetails; + } + + throw new AssertionFailure( "Unrecognized entity name: " + entityName ); + } + + @Override + public DiscriminatorValueDetails getDetailsForDiscriminatorValue(Object value) { + if ( value == null ) { + return discriminatorValueToEntityNameMap.get( NULL_DISCRIMINATOR ); + } + + final DiscriminatorValueDetails valueMatch = discriminatorValueToEntityNameMap.get( value ); + if ( valueMatch != null ) { + return valueMatch; + } + + final DiscriminatorValueDetails notNullMatch = discriminatorValueToEntityNameMap.get( NOT_NULL_DISCRIMINATOR ); + if ( notNullMatch != null ) { + return notNullMatch; + } + + throw new HibernateException( "Unrecognized discriminator value: " + value ); + } + + @Override + public void forEachValueDetail(Consumer consumer) { + discriminatorValueToEntityNameMap.forEach( (value, detail) -> consumer.accept( detail ) ); + } + + @Override + public X fromValueDetails(Function handler) { + for ( DiscriminatorValueDetails detail : discriminatorValueToEntityNameMap.values() ) { + final X result = handler.apply( detail ); + if ( result != null ) { + return result; + } + } + return null; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/AnyDiscriminatorPart.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/AnyDiscriminatorPart.java index e9b49f0601..90243d6ce2 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/AnyDiscriminatorPart.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/AnyDiscriminatorPart.java @@ -15,15 +15,18 @@ import org.hibernate.engine.FetchTiming; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.internal.util.IndexedConsumer; +import org.hibernate.metamodel.mapping.DefaultDiscriminatorConverter; import org.hibernate.metamodel.mapping.DiscriminatedAssociationModelPart; import org.hibernate.metamodel.mapping.DiscriminatorConverter; import org.hibernate.metamodel.mapping.DiscriminatorMapping; import org.hibernate.metamodel.mapping.EntityDiscriminatorMapping; import org.hibernate.metamodel.mapping.EntityMappingType; import org.hibernate.metamodel.mapping.JdbcMapping; +import org.hibernate.metamodel.mapping.MappedDiscriminatorConverter; import org.hibernate.metamodel.mapping.MappingType; import org.hibernate.metamodel.mapping.SelectableConsumer; import org.hibernate.metamodel.model.domain.NavigableRole; +import org.hibernate.metamodel.spi.MappingMetamodelImplementor; import org.hibernate.spi.NavigablePath; import org.hibernate.sql.ast.spi.FromClauseAccess; import org.hibernate.sql.ast.spi.SqlAstCreationState; @@ -39,6 +42,7 @@ import org.hibernate.sql.results.graph.FetchOptions; import org.hibernate.sql.results.graph.FetchParent; import org.hibernate.sql.results.graph.basic.BasicFetch; import org.hibernate.type.BasicType; +import org.hibernate.type.descriptor.converter.spi.BasicValueConverter; import org.hibernate.type.descriptor.java.ClassJavaType; import org.hibernate.type.descriptor.java.JavaType; @@ -81,7 +85,7 @@ public class AnyDiscriminatorPart implements DiscriminatorMapping, FetchOptions boolean partitioned, BasicType underlyingJdbcMapping, Map valueToEntityNameMap, - SessionFactoryImplementor sessionFactory) { + MappingMetamodelImplementor mappingMetamodel) { this.navigableRole = partRole; this.declaringType = declaringType; this.table = table; @@ -95,13 +99,20 @@ public class AnyDiscriminatorPart implements DiscriminatorMapping, FetchOptions this.partitioned = partitioned; this.underlyingJdbcMapping = underlyingJdbcMapping; - this.valueConverter = DiscriminatorConverter.fromValueMappings( - partRole, - ClassJavaType.INSTANCE, - underlyingJdbcMapping, - valueToEntityNameMap, - sessionFactory - ); + this.valueConverter = valueToEntityNameMap.isEmpty() + ? DefaultDiscriminatorConverter.fromMappingMetamodel( + partRole, + ClassJavaType.INSTANCE, + underlyingJdbcMapping, + mappingMetamodel + ) + : MappedDiscriminatorConverter.fromValueMappings( + partRole, + ClassJavaType.INSTANCE, + underlyingJdbcMapping, + valueToEntityNameMap, + mappingMetamodel + ); } public DiscriminatorConverter getValueConverter() { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/DiscriminatedAssociationMapping.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/DiscriminatedAssociationMapping.java index be27357fd2..fd87390664 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/DiscriminatedAssociationMapping.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/DiscriminatedAssociationMapping.java @@ -95,7 +95,7 @@ public class DiscriminatedAssociationMapping implements MappingType, FetchOption bootValueMapping.isPartitionKey(), (BasicType) metaType.getBaseType(), metaType.getDiscriminatorValuesToEntityNameMap(), - creationProcess.getCreationContext().getSessionFactory() + creationProcess.getCreationContext().getSessionFactory().getMappingMetamodel() ); @@ -163,14 +163,18 @@ public class DiscriminatedAssociationMapping implements MappingType, FetchOption } public Object resolveDiscriminatorValueToEntityMapping(EntityMappingType entityMappingType) { - final DiscriminatorValueDetails details = discriminatorPart.getValueConverter().getDetailsForEntityName( entityMappingType.getEntityName() ); + final DiscriminatorValueDetails details = + discriminatorPart.getValueConverter() + .getDetailsForEntityName( entityMappingType.getEntityName() ); return details != null ? details.getValue() : null; } public EntityMappingType resolveDiscriminatorValueToEntityMapping(Object discriminatorValue) { - final DiscriminatorValueDetails details = discriminatorPart.getValueConverter().getDetailsForDiscriminatorValue( discriminatorValue ); + final DiscriminatorValueDetails details = + discriminatorPart.getValueConverter(). + getDetailsForDiscriminatorValue( discriminatorValue ); return details != null ? details.getIndicatedEntity() : null; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AnyMappingDomainTypeImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AnyMappingDomainTypeImpl.java index 0c8e313c2d..7e2b9c2d43 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AnyMappingDomainTypeImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AnyMappingDomainTypeImpl.java @@ -6,26 +6,22 @@ */ package org.hibernate.metamodel.model.domain.internal; -import java.util.List; - -import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.mapping.Any; import org.hibernate.mapping.Column; -import org.hibernate.metamodel.MappingMetamodel; -import org.hibernate.metamodel.mapping.DiscriminatorConverter; -import org.hibernate.metamodel.mapping.internal.AnyDiscriminatorPart; +import org.hibernate.metamodel.mapping.DefaultDiscriminatorConverter; +import org.hibernate.metamodel.mapping.MappedDiscriminatorConverter; import org.hibernate.metamodel.model.domain.AnyMappingDomainType; import org.hibernate.metamodel.model.domain.NavigableRole; import org.hibernate.metamodel.model.domain.SimpleDomainType; +import org.hibernate.metamodel.spi.MappingMetamodelImplementor; import org.hibernate.type.AnyType; import org.hibernate.type.BasicType; import org.hibernate.type.MetaType; -import org.hibernate.type.descriptor.converter.spi.BasicValueConverter; import org.hibernate.type.descriptor.java.ClassJavaType; import org.hibernate.type.descriptor.java.JavaType; -import org.hibernate.type.descriptor.java.ObjectJavaType; import org.hibernate.type.internal.ConvertedBasicTypeImpl; -import org.hibernate.type.spi.TypeConfiguration; + +import java.util.List; /** * @author Steve Ebersole @@ -40,9 +36,7 @@ public class AnyMappingDomainTypeImpl implements AnyMappingDomainType { Any bootAnyMapping, AnyType anyType, JavaType baseJtd, - TypeConfiguration typeConfiguration, - MappingMetamodel mappingMetamodel, - SessionFactoryImplementor sessionFactory) { + MappingMetamodelImplementor mappingMetamodel) { this.anyType = anyType; this.baseJtd = baseJtd; @@ -53,12 +47,19 @@ public class AnyMappingDomainTypeImpl implements AnyMappingDomainType { anyDiscriminatorType = new ConvertedBasicTypeImpl<>( navigableRole.getFullPath(), discriminatorBaseType.getJdbcType(), - DiscriminatorConverter.fromValueMappings( + bootAnyMapping.getMetaValues().isEmpty() + ? DefaultDiscriminatorConverter.fromMappingMetamodel( navigableRole, - (JavaType) ClassJavaType.INSTANCE, + ClassJavaType.INSTANCE, + discriminatorBaseType, + mappingMetamodel + ) + : MappedDiscriminatorConverter.fromValueMappings( + navigableRole, + ClassJavaType.INSTANCE, discriminatorBaseType, bootAnyMapping.getMetaValues(), - sessionFactory + mappingMetamodel ) ); } diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index 3dc6fe1100..e1c2f2e982 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -167,6 +167,7 @@ import org.hibernate.metamodel.mapping.EntityVersionMapping; import org.hibernate.metamodel.mapping.ForeignKeyDescriptor; import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.ManagedMappingType; +import org.hibernate.metamodel.mapping.MappedDiscriminatorConverter; import org.hibernate.metamodel.mapping.MappingModelHelper; import org.hibernate.metamodel.mapping.ModelPart; import org.hibernate.metamodel.mapping.NaturalIdMapping; @@ -2274,12 +2275,12 @@ public abstract class AbstractEntityPersister } //noinspection rawtypes - final DiscriminatorConverter converter = DiscriminatorConverter.fromValueMappings( + final DiscriminatorConverter converter = MappedDiscriminatorConverter.fromValueMappings( getNavigableRole().append( EntityDiscriminatorMapping.ROLE_NAME ), domainJavaType, underlingJdbcMapping, getSubclassByDiscriminatorValue(), - factory + factory.getMappingMetamodel() ); //noinspection unchecked,rawtypes