Search in sources :

Example 1 with DiscriminatorType

use of org.hibernate.type.DiscriminatorType in project hibernate-orm by hibernate.

the class ModelBinder method bindAny.

private void bindAny(MappingDocument sourceDocument, final AnyMappingSource anyMapping, Any anyBinding, final AttributeRole attributeRole, AttributePath attributePath) {
    final TypeResolution keyTypeResolution = resolveType(sourceDocument, anyMapping.getKeySource().getTypeSource());
    if (keyTypeResolution != null) {
        anyBinding.setIdentifierType(keyTypeResolution.typeName);
    }
    final TypeResolution discriminatorTypeResolution = resolveType(sourceDocument, anyMapping.getDiscriminatorSource().getTypeSource());
    if (discriminatorTypeResolution != null) {
        anyBinding.setMetaType(discriminatorTypeResolution.typeName);
        try {
            final DiscriminatorType metaType = (DiscriminatorType) sourceDocument.getMetadataCollector().getTypeResolver().heuristicType(discriminatorTypeResolution.typeName);
            final HashMap anyValueBindingMap = new HashMap();
            for (Map.Entry<String, String> discriminatorValueMappings : anyMapping.getDiscriminatorSource().getValueMappings().entrySet()) {
                try {
                    final Object discriminatorValue = metaType.stringToObject(discriminatorValueMappings.getKey());
                    final String mappedEntityName = sourceDocument.qualifyClassName(discriminatorValueMappings.getValue());
                    // noinspection unchecked
                    anyValueBindingMap.put(discriminatorValue, mappedEntityName);
                } catch (Exception e) {
                    throw new MappingException(String.format(Locale.ENGLISH, "Unable to interpret <meta-value value=\"%s\" class=\"%s\"/> defined as part of <any/> attribute [%s]", discriminatorValueMappings.getKey(), discriminatorValueMappings.getValue(), attributeRole.getFullPath()), e, sourceDocument.getOrigin());
                }
            }
            anyBinding.setMetaValues(anyValueBindingMap);
        } catch (ClassCastException e) {
            throw new MappingException(String.format(Locale.ENGLISH, "Specified meta-type [%s] for <any/> attribute [%s] did not implement DiscriminatorType", discriminatorTypeResolution.typeName, attributeRole.getFullPath()), e, sourceDocument.getOrigin());
        }
    }
    relationalObjectBinder.bindColumnOrFormula(sourceDocument, anyMapping.getDiscriminatorSource().getRelationalValueSource(), anyBinding, true, new RelationalObjectBinder.ColumnNamingDelegate() {

        @Override
        public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
            return implicitNamingStrategy.determineAnyDiscriminatorColumnName(anyMapping.getDiscriminatorSource());
        }
    });
    relationalObjectBinder.bindColumnsAndFormulas(sourceDocument, anyMapping.getKeySource().getRelationalValueSources(), anyBinding, true, new RelationalObjectBinder.ColumnNamingDelegate() {

        @Override
        public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
            return implicitNamingStrategy.determineAnyKeyColumnName(anyMapping.getKeySource());
        }
    });
}
Also used : HashMap(java.util.HashMap) MappingException(org.hibernate.boot.MappingException) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) MappingException(org.hibernate.boot.MappingException) Identifier(org.hibernate.boot.model.naming.Identifier) DiscriminatorType(org.hibernate.type.DiscriminatorType) LocalMetadataBuildingContext(org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 MappingException (org.hibernate.boot.MappingException)1 Identifier (org.hibernate.boot.model.naming.Identifier)1 LocalMetadataBuildingContext (org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext)1 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)1 DiscriminatorType (org.hibernate.type.DiscriminatorType)1