HHH-16783 split @Any discriminator handling into two subclasses

This commit is contained in:
Gavin 2023-06-12 22:39:12 +02:00 committed by Gavin King
parent 925d09528d
commit 3a8e66d600
8 changed files with 285 additions and 153 deletions

View File

@ -252,7 +252,8 @@ public class AttributeFactory {
final org.hibernate.type.Type type = typeContext.getHibernateValue().getType(); final org.hibernate.type.Type type = typeContext.getHibernateValue().getType();
if ( type instanceof EntityType ) { if ( type instanceof EntityType ) {
final EntityType entityType = (EntityType) type; final EntityType entityType = (EntityType) type;
final IdentifiableDomainType<Y> domainType = context.locateIdentifiableType( entityType.getAssociatedEntityName() ); final IdentifiableDomainType<Y> domainType =
context.locateIdentifiableType( entityType.getAssociatedEntityName() );
if ( domainType == null ) { if ( domainType == null ) {
// Due to the use of generics, it can happen that a mapped super class uses a type // 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 // 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(), (Any) typeContext.getHibernateValue(),
anyType, anyType,
(JavaType<Class>) baseJtd, (JavaType<Class>) baseJtd,
context.getTypeConfiguration(), context.getRuntimeModelCreationContext().getSessionFactory().getMappingMetamodel()
context.getMetamodel(),
context.getRuntimeModelCreationContext().getSessionFactory()
); );
} }
case EMBEDDABLE: { case EMBEDDABLE: {
@ -344,7 +343,8 @@ public class AttributeFactory {
DomainType<?> metaModelType, DomainType<?> metaModelType,
MetadataContext context) { MetadataContext context) {
if ( typeContext.getValueClassification() == ValueClassification.BASIC ) { if ( typeContext.getValueClassification() == ValueClassification.BASIC ) {
final ConverterDescriptor descriptor = ( (SimpleValue) typeContext.getHibernateValue() ).getJpaAttributeConverterDescriptor(); final ConverterDescriptor descriptor =
( (SimpleValue) typeContext.getHibernateValue() ).getJpaAttributeConverterDescriptor();
if ( descriptor != null ) { if ( descriptor != null ) {
return context.getJavaTypeRegistry().resolveDescriptor( return context.getJavaTypeRegistry().resolveDescriptor(
descriptor.getRelationalValueResolvedType().getErasedType() descriptor.getRelationalValueResolvedType().getErasedType()

View File

@ -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<O,R> extends DiscriminatorConverter<O,R> {
public static <O,R> DefaultDiscriminatorConverter<O,R> fromMappingMetamodel(
NavigableRole role,
JavaType<O> domainJavaType,
BasicType<R> underlyingJdbcMapping,
MappingMetamodelImplementor mappingMetamodel) {
return new DefaultDiscriminatorConverter<>(
role,
domainJavaType,
underlyingJdbcMapping.getJavaTypeDescriptor(),
mappingMetamodel
);
}
private final MappingMetamodelImplementor mappingMetamodel;
public DefaultDiscriminatorConverter(
NavigableRole discriminatorRole,
JavaType<O> domainJavaType,
JavaType<R> 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<DiscriminatorValueDetails> consumer) {
}
@Override
public <X> X fromValueDetails(Function<DiscriminatorValueDetails,X> handler) {
return null;
}
}

View File

@ -6,94 +6,31 @@
*/ */
package org.hibernate.metamodel.mapping; 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.RepresentationMode;
import org.hibernate.metamodel.mapping.internal.DiscriminatorValueDetailsImpl;
import org.hibernate.metamodel.model.domain.NavigableRole; 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.converter.spi.BasicValueConverter;
import org.hibernate.type.descriptor.java.JavaType; import org.hibernate.type.descriptor.java.JavaType;
import static org.hibernate.persister.entity.DiscriminatorHelper.NOT_NULL_DISCRIMINATOR; import java.util.function.Consumer;
import static org.hibernate.persister.entity.DiscriminatorHelper.NULL_DISCRIMINATOR; 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 <O> The domain type - either <ul>
* <li>
* the {@linkplain EntityMappingType#getMappedJavaType() entity Class} for unnamed entities
* </li>
* <li>
* the {@linkplain EntityMappingType#getEntityName() entity name} for named entities
* </li>
* </ul>
* @param <R> The Java type of the relational form of the discriminator
*
* @author Steve Ebersole * @author Steve Ebersole
* @author Gavin King
*/ */
public class DiscriminatorConverter<O,R> implements BasicValueConverter<O,R> { public abstract class DiscriminatorConverter<O,R> implements BasicValueConverter<O,R> {
public static <O,R> DiscriminatorConverter<O,R> fromValueMappings(
NavigableRole role,
JavaType<O> domainJavaType,
BasicType<R> underlyingJdbcMapping,
Map<Object,String> valueMappings,
SessionFactoryImplementor sessionFactory) {
final MappingMetamodelImplementor mappingMetamodel = sessionFactory.getMappingMetamodel();
final List<DiscriminatorValueDetails> 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
);
}
private final NavigableRole discriminatorRole; private final NavigableRole discriminatorRole;
private final JavaType<O> domainJavaType; private final JavaType<O> domainJavaType;
private final JavaType<R> relationalJavaType; private final JavaType<R> relationalJavaType;
private final Map<Object, DiscriminatorValueDetails> discriminatorValueToEntityNameMap;
private final Map<String,DiscriminatorValueDetails> entityNameToDiscriminatorValueMap;
private final MappingMetamodelImplementor mappingMetamodel;
public DiscriminatorConverter( public DiscriminatorConverter(
NavigableRole discriminatorRole, NavigableRole discriminatorRole,
JavaType<O> domainJavaType, JavaType<O> domainJavaType,
JavaType<R> relationalJavaType, JavaType<R> relationalJavaType) {
List<DiscriminatorValueDetails> valueMappings,
MappingMetamodelImplementor mappingMetamodel) {
this.discriminatorRole = discriminatorRole; this.discriminatorRole = discriminatorRole;
this.domainJavaType = domainJavaType; this.domainJavaType = domainJavaType;
this.relationalJavaType = relationalJavaType; 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() { public NavigableRole getNavigableRole() {
@ -131,46 +68,6 @@ public class DiscriminatorConverter<O,R> implements BasicValueConverter<O,R> {
: (O) indicatedEntity.getEntityName(); : (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 @Override
public R toRelationalValue(O domainForm) { public R toRelationalValue(O domainForm) {
assert domainForm == null || domainForm instanceof String || domainForm instanceof Class; assert domainForm == null || domainForm instanceof String || domainForm instanceof Class;
@ -192,22 +89,16 @@ public class DiscriminatorConverter<O,R> implements BasicValueConverter<O,R> {
return (R) discriminatorValueDetails.getValue(); return (R) discriminatorValueDetails.getValue();
} }
public abstract DiscriminatorValueDetails getDetailsForDiscriminatorValue(Object relationalForm);
public abstract DiscriminatorValueDetails getDetailsForEntityName(String entityName);
@Override @Override
public String toString() { public String toString() {
return "DiscriminatorConverter(" + discriminatorRole.getFullPath() + ")"; return "DiscriminatorConverter(" + discriminatorRole.getFullPath() + ")";
} }
public void forEachValueDetail(Consumer<DiscriminatorValueDetails> consumer) { public abstract void forEachValueDetail(Consumer<DiscriminatorValueDetails> consumer);
discriminatorValueToEntityNameMap.forEach( (value, detail) -> consumer.accept( detail ) );
}
public <X> X fromValueDetails(Function<DiscriminatorValueDetails,X> handler) { public abstract <X> X fromValueDetails(Function<DiscriminatorValueDetails,X> handler);
for ( DiscriminatorValueDetails detail : discriminatorValueToEntityNameMap.values() ) {
final X result = handler.apply( detail );
if ( result != null ) {
return result;
}
}
return null;
}
} }

View File

@ -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 <O> The domain type - either <ul>
* <li>
* the {@linkplain EntityMappingType#getMappedJavaType() entity Class} for unnamed entities
* </li>
* <li>
* the {@linkplain EntityMappingType#getEntityName() entity name} for named entities
* </li>
* </ul>
* @param <R> The Java type of the relational form of the discriminator
*
* @author Steve Ebersole
*/
public class MappedDiscriminatorConverter<O,R> extends DiscriminatorConverter<O,R> {
public static <O,R> MappedDiscriminatorConverter<O,R> fromValueMappings(
NavigableRole role,
JavaType<O> domainJavaType,
BasicType<R> underlyingJdbcMapping,
Map<Object,String> valueMappings,
MappingMetamodelImplementor mappingMetamodel) {
final List<DiscriminatorValueDetails> 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<Object, DiscriminatorValueDetails> discriminatorValueToEntityNameMap;
private final Map<String,DiscriminatorValueDetails> entityNameToDiscriminatorValueMap;
public MappedDiscriminatorConverter(
NavigableRole discriminatorRole,
JavaType<O> domainJavaType,
JavaType<R> relationalJavaType,
List<DiscriminatorValueDetails> 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<DiscriminatorValueDetails> consumer) {
discriminatorValueToEntityNameMap.forEach( (value, detail) -> consumer.accept( detail ) );
}
@Override
public <X> X fromValueDetails(Function<DiscriminatorValueDetails,X> handler) {
for ( DiscriminatorValueDetails detail : discriminatorValueToEntityNameMap.values() ) {
final X result = handler.apply( detail );
if ( result != null ) {
return result;
}
}
return null;
}
}

View File

@ -15,15 +15,18 @@ import org.hibernate.engine.FetchTiming;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.IndexedConsumer; import org.hibernate.internal.util.IndexedConsumer;
import org.hibernate.metamodel.mapping.DefaultDiscriminatorConverter;
import org.hibernate.metamodel.mapping.DiscriminatedAssociationModelPart; import org.hibernate.metamodel.mapping.DiscriminatedAssociationModelPart;
import org.hibernate.metamodel.mapping.DiscriminatorConverter; import org.hibernate.metamodel.mapping.DiscriminatorConverter;
import org.hibernate.metamodel.mapping.DiscriminatorMapping; import org.hibernate.metamodel.mapping.DiscriminatorMapping;
import org.hibernate.metamodel.mapping.EntityDiscriminatorMapping; import org.hibernate.metamodel.mapping.EntityDiscriminatorMapping;
import org.hibernate.metamodel.mapping.EntityMappingType; import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.MappedDiscriminatorConverter;
import org.hibernate.metamodel.mapping.MappingType; import org.hibernate.metamodel.mapping.MappingType;
import org.hibernate.metamodel.mapping.SelectableConsumer; import org.hibernate.metamodel.mapping.SelectableConsumer;
import org.hibernate.metamodel.model.domain.NavigableRole; import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
import org.hibernate.spi.NavigablePath; import org.hibernate.spi.NavigablePath;
import org.hibernate.sql.ast.spi.FromClauseAccess; import org.hibernate.sql.ast.spi.FromClauseAccess;
import org.hibernate.sql.ast.spi.SqlAstCreationState; 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.FetchParent;
import org.hibernate.sql.results.graph.basic.BasicFetch; import org.hibernate.sql.results.graph.basic.BasicFetch;
import org.hibernate.type.BasicType; 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.ClassJavaType;
import org.hibernate.type.descriptor.java.JavaType; import org.hibernate.type.descriptor.java.JavaType;
@ -81,7 +85,7 @@ public class AnyDiscriminatorPart implements DiscriminatorMapping, FetchOptions
boolean partitioned, boolean partitioned,
BasicType<?> underlyingJdbcMapping, BasicType<?> underlyingJdbcMapping,
Map<Object,String> valueToEntityNameMap, Map<Object,String> valueToEntityNameMap,
SessionFactoryImplementor sessionFactory) { MappingMetamodelImplementor mappingMetamodel) {
this.navigableRole = partRole; this.navigableRole = partRole;
this.declaringType = declaringType; this.declaringType = declaringType;
this.table = table; this.table = table;
@ -95,12 +99,19 @@ public class AnyDiscriminatorPart implements DiscriminatorMapping, FetchOptions
this.partitioned = partitioned; this.partitioned = partitioned;
this.underlyingJdbcMapping = underlyingJdbcMapping; this.underlyingJdbcMapping = underlyingJdbcMapping;
this.valueConverter = DiscriminatorConverter.fromValueMappings( this.valueConverter = valueToEntityNameMap.isEmpty()
? DefaultDiscriminatorConverter.fromMappingMetamodel(
partRole,
ClassJavaType.INSTANCE,
underlyingJdbcMapping,
mappingMetamodel
)
: MappedDiscriminatorConverter.fromValueMappings(
partRole, partRole,
ClassJavaType.INSTANCE, ClassJavaType.INSTANCE,
underlyingJdbcMapping, underlyingJdbcMapping,
valueToEntityNameMap, valueToEntityNameMap,
sessionFactory mappingMetamodel
); );
} }

View File

@ -95,7 +95,7 @@ public class DiscriminatedAssociationMapping implements MappingType, FetchOption
bootValueMapping.isPartitionKey(), bootValueMapping.isPartitionKey(),
(BasicType<?>) metaType.getBaseType(), (BasicType<?>) metaType.getBaseType(),
metaType.getDiscriminatorValuesToEntityNameMap(), metaType.getDiscriminatorValuesToEntityNameMap(),
creationProcess.getCreationContext().getSessionFactory() creationProcess.getCreationContext().getSessionFactory().getMappingMetamodel()
); );
@ -163,14 +163,18 @@ public class DiscriminatedAssociationMapping implements MappingType, FetchOption
} }
public Object resolveDiscriminatorValueToEntityMapping(EntityMappingType entityMappingType) { public Object resolveDiscriminatorValueToEntityMapping(EntityMappingType entityMappingType) {
final DiscriminatorValueDetails details = discriminatorPart.getValueConverter().getDetailsForEntityName( entityMappingType.getEntityName() ); final DiscriminatorValueDetails details =
discriminatorPart.getValueConverter()
.getDetailsForEntityName( entityMappingType.getEntityName() );
return details != null return details != null
? details.getValue() ? details.getValue()
: null; : null;
} }
public EntityMappingType resolveDiscriminatorValueToEntityMapping(Object discriminatorValue) { public EntityMappingType resolveDiscriminatorValueToEntityMapping(Object discriminatorValue) {
final DiscriminatorValueDetails details = discriminatorPart.getValueConverter().getDetailsForDiscriminatorValue( discriminatorValue ); final DiscriminatorValueDetails details =
discriminatorPart.getValueConverter().
getDetailsForDiscriminatorValue( discriminatorValue );
return details != null return details != null
? details.getIndicatedEntity() ? details.getIndicatedEntity()
: null; : null;

View File

@ -6,26 +6,22 @@
*/ */
package org.hibernate.metamodel.model.domain.internal; 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.Any;
import org.hibernate.mapping.Column; import org.hibernate.mapping.Column;
import org.hibernate.metamodel.MappingMetamodel; import org.hibernate.metamodel.mapping.DefaultDiscriminatorConverter;
import org.hibernate.metamodel.mapping.DiscriminatorConverter; import org.hibernate.metamodel.mapping.MappedDiscriminatorConverter;
import org.hibernate.metamodel.mapping.internal.AnyDiscriminatorPart;
import org.hibernate.metamodel.model.domain.AnyMappingDomainType; import org.hibernate.metamodel.model.domain.AnyMappingDomainType;
import org.hibernate.metamodel.model.domain.NavigableRole; import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.metamodel.model.domain.SimpleDomainType; import org.hibernate.metamodel.model.domain.SimpleDomainType;
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
import org.hibernate.type.AnyType; import org.hibernate.type.AnyType;
import org.hibernate.type.BasicType; import org.hibernate.type.BasicType;
import org.hibernate.type.MetaType; 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.ClassJavaType;
import org.hibernate.type.descriptor.java.JavaType; import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.java.ObjectJavaType;
import org.hibernate.type.internal.ConvertedBasicTypeImpl; import org.hibernate.type.internal.ConvertedBasicTypeImpl;
import org.hibernate.type.spi.TypeConfiguration;
import java.util.List;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
@ -40,9 +36,7 @@ public class AnyMappingDomainTypeImpl implements AnyMappingDomainType<Class> {
Any bootAnyMapping, Any bootAnyMapping,
AnyType anyType, AnyType anyType,
JavaType<Class> baseJtd, JavaType<Class> baseJtd,
TypeConfiguration typeConfiguration, MappingMetamodelImplementor mappingMetamodel) {
MappingMetamodel mappingMetamodel,
SessionFactoryImplementor sessionFactory) {
this.anyType = anyType; this.anyType = anyType;
this.baseJtd = baseJtd; this.baseJtd = baseJtd;
@ -53,12 +47,19 @@ public class AnyMappingDomainTypeImpl implements AnyMappingDomainType<Class> {
anyDiscriminatorType = new ConvertedBasicTypeImpl<>( anyDiscriminatorType = new ConvertedBasicTypeImpl<>(
navigableRole.getFullPath(), navigableRole.getFullPath(),
discriminatorBaseType.getJdbcType(), discriminatorBaseType.getJdbcType(),
DiscriminatorConverter.fromValueMappings( bootAnyMapping.getMetaValues().isEmpty()
? DefaultDiscriminatorConverter.fromMappingMetamodel(
navigableRole, navigableRole,
(JavaType) ClassJavaType.INSTANCE, ClassJavaType.INSTANCE,
discriminatorBaseType,
mappingMetamodel
)
: MappedDiscriminatorConverter.fromValueMappings(
navigableRole,
ClassJavaType.INSTANCE,
discriminatorBaseType, discriminatorBaseType,
bootAnyMapping.getMetaValues(), bootAnyMapping.getMetaValues(),
sessionFactory mappingMetamodel
) )
); );
} }

View File

@ -167,6 +167,7 @@ import org.hibernate.metamodel.mapping.EntityVersionMapping;
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor; import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.ManagedMappingType; import org.hibernate.metamodel.mapping.ManagedMappingType;
import org.hibernate.metamodel.mapping.MappedDiscriminatorConverter;
import org.hibernate.metamodel.mapping.MappingModelHelper; import org.hibernate.metamodel.mapping.MappingModelHelper;
import org.hibernate.metamodel.mapping.ModelPart; import org.hibernate.metamodel.mapping.ModelPart;
import org.hibernate.metamodel.mapping.NaturalIdMapping; import org.hibernate.metamodel.mapping.NaturalIdMapping;
@ -2274,12 +2275,12 @@ public abstract class AbstractEntityPersister
} }
//noinspection rawtypes //noinspection rawtypes
final DiscriminatorConverter converter = DiscriminatorConverter.fromValueMappings( final DiscriminatorConverter converter = MappedDiscriminatorConverter.fromValueMappings(
getNavigableRole().append( EntityDiscriminatorMapping.ROLE_NAME ), getNavigableRole().append( EntityDiscriminatorMapping.ROLE_NAME ),
domainJavaType, domainJavaType,
underlingJdbcMapping, underlingJdbcMapping,
getSubclassByDiscriminatorValue(), getSubclassByDiscriminatorValue(),
factory factory.getMappingMetamodel()
); );
//noinspection unchecked,rawtypes //noinspection unchecked,rawtypes