Search in sources :

Example 1 with LocalMetadataBuildingContext

use of org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext in project hibernate-orm by hibernate.

the class ModelBinder method bindEntityDiscriminator.

private void bindEntityDiscriminator(MappingDocument sourceDocument, final EntityHierarchySourceImpl hierarchySource, RootClass rootEntityDescriptor) {
    final SimpleValue discriminatorValue = new SimpleValue(sourceDocument, rootEntityDescriptor.getTable());
    rootEntityDescriptor.setDiscriminator(discriminatorValue);
    String typeName = hierarchySource.getDiscriminatorSource().getExplicitHibernateTypeName();
    if (typeName == null) {
        typeName = "string";
    }
    bindSimpleValueType(sourceDocument, new HibernateTypeSourceImpl(typeName), discriminatorValue);
    relationalObjectBinder.bindColumnOrFormula(sourceDocument, hierarchySource.getDiscriminatorSource().getDiscriminatorRelationalValueSource(), discriminatorValue, false, new RelationalObjectBinder.ColumnNamingDelegate() {

        @Override
        public Identifier determineImplicitName(final LocalMetadataBuildingContext context) {
            return implicitNamingStrategy.determineDiscriminatorColumnName(hierarchySource.getDiscriminatorSource());
        }
    });
    rootEntityDescriptor.setPolymorphic(true);
    rootEntityDescriptor.setDiscriminatorInsertable(hierarchySource.getDiscriminatorSource().isInserted());
    // todo : currently isForced() is defined as boolean, not Boolean
    // although it has always been that way (DTD too)
    final boolean force = hierarchySource.getDiscriminatorSource().isForced() || sourceDocument.getBuildingOptions().shouldImplicitlyForceDiscriminatorInSelect();
    rootEntityDescriptor.setForceDiscriminator(force);
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) LocalMetadataBuildingContext(org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext) SimpleValue(org.hibernate.mapping.SimpleValue)

Example 2 with LocalMetadataBuildingContext

use of org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext in project hibernate-orm by hibernate.

the class ModelBinder method bindListOrArrayIndex.

public void bindListOrArrayIndex(MappingDocument mappingDocument, final IndexedPluralAttributeSource attributeSource, org.hibernate.mapping.List collectionBinding) {
    final PluralAttributeSequentialIndexSource indexSource = (PluralAttributeSequentialIndexSource) attributeSource.getIndexSource();
    final SimpleValue indexBinding = new SimpleValue(mappingDocument, collectionBinding.getCollectionTable());
    bindSimpleValueType(mappingDocument, indexSource.getTypeInformation(), indexBinding);
    relationalObjectBinder.bindColumnsAndFormulas(mappingDocument, indexSource.getRelationalValueSources(), indexBinding, attributeSource.getElementSource() instanceof PluralAttributeElementSourceOneToMany, new RelationalObjectBinder.ColumnNamingDelegate() {

        @Override
        public Identifier determineImplicitName(final LocalMetadataBuildingContext context) {
            return context.getBuildingOptions().getImplicitNamingStrategy().determineListIndexColumnName(new ImplicitIndexColumnNameSource() {

                @Override
                public AttributePath getPluralAttributePath() {
                    return attributeSource.getAttributePath();
                }

                @Override
                public MetadataBuildingContext getBuildingContext() {
                    return context;
                }
            });
        }
    });
    collectionBinding.setIndex(indexBinding);
    collectionBinding.setBaseIndex(indexSource.getBase());
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) PluralAttributeElementSourceOneToMany(org.hibernate.boot.model.source.spi.PluralAttributeElementSourceOneToMany) PluralAttributeSequentialIndexSource(org.hibernate.boot.model.source.spi.PluralAttributeSequentialIndexSource) LocalMetadataBuildingContext(org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext) ImplicitIndexColumnNameSource(org.hibernate.boot.model.naming.ImplicitIndexColumnNameSource) SimpleValue(org.hibernate.mapping.SimpleValue)

Example 3 with LocalMetadataBuildingContext

use of org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext in project hibernate-orm by hibernate.

the class ModelBinder method bindSimpleEntityIdentifier.

private void bindSimpleEntityIdentifier(MappingDocument sourceDocument, final EntityHierarchySourceImpl hierarchySource, RootClass rootEntityDescriptor) {
    final IdentifierSourceSimple idSource = (IdentifierSourceSimple) hierarchySource.getIdentifierSource();
    final SimpleValue idValue = new SimpleValue(sourceDocument, rootEntityDescriptor.getTable());
    rootEntityDescriptor.setIdentifier(idValue);
    bindSimpleValueType(sourceDocument, idSource.getIdentifierAttributeSource().getTypeInformation(), idValue);
    final String propertyName = idSource.getIdentifierAttributeSource().getName();
    if (propertyName == null || !rootEntityDescriptor.hasPojoRepresentation()) {
        if (!idValue.isTypeSpecified()) {
            throw new MappingException("must specify an identifier type: " + rootEntityDescriptor.getEntityName(), sourceDocument.getOrigin());
        }
    } else {
        idValue.setTypeUsingReflection(rootEntityDescriptor.getClassName(), propertyName);
    }
    relationalObjectBinder.bindColumnsAndFormulas(sourceDocument, ((RelationalValueSourceContainer) idSource.getIdentifierAttributeSource()).getRelationalValueSources(), idValue, false, new RelationalObjectBinder.ColumnNamingDelegate() {

        @Override
        public Identifier determineImplicitName(final LocalMetadataBuildingContext context) {
            context.getBuildingOptions().getImplicitNamingStrategy().determineIdentifierColumnName(new ImplicitIdentifierColumnNameSource() {

                @Override
                public EntityNaming getEntityNaming() {
                    return hierarchySource.getRoot().getEntityNamingSource();
                }

                @Override
                public AttributePath getIdentifierAttributePath() {
                    return idSource.getIdentifierAttributeSource().getAttributePath();
                }

                @Override
                public MetadataBuildingContext getBuildingContext() {
                    return context;
                }
            });
            return database.toIdentifier(propertyName);
        }
    });
    if (propertyName != null) {
        Property prop = new Property();
        prop.setValue(idValue);
        bindProperty(sourceDocument, idSource.getIdentifierAttributeSource(), prop);
        rootEntityDescriptor.setIdentifierProperty(prop);
        rootEntityDescriptor.setDeclaredIdentifierProperty(prop);
    }
    makeIdentifier(sourceDocument, idSource.getIdentifierGeneratorDescriptor(), idSource.getUnsavedValue(), idValue);
}
Also used : ImplicitIdentifierColumnNameSource(org.hibernate.boot.model.naming.ImplicitIdentifierColumnNameSource) Identifier(org.hibernate.boot.model.naming.Identifier) IdentifierSourceSimple(org.hibernate.boot.model.source.spi.IdentifierSourceSimple) LocalMetadataBuildingContext(org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext) Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty) SimpleValue(org.hibernate.mapping.SimpleValue) MappingException(org.hibernate.boot.MappingException)

Example 4 with LocalMetadataBuildingContext

use of org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext 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)

Example 5 with LocalMetadataBuildingContext

use of org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext in project hibernate-orm by hibernate.

the class ModelBinder method bindSecondaryTable.

private void bindSecondaryTable(MappingDocument mappingDocument, SecondaryTableSource secondaryTableSource, Join secondaryTableJoin, final EntityTableXref entityTableXref) {
    final PersistentClass persistentClass = secondaryTableJoin.getPersistentClass();
    final Identifier catalogName = determineCatalogName(secondaryTableSource.getTableSource());
    final Identifier schemaName = determineSchemaName(secondaryTableSource.getTableSource());
    final Namespace namespace = database.locateNamespace(catalogName, schemaName);
    Table secondaryTable;
    final Identifier logicalTableName;
    if (TableSource.class.isInstance(secondaryTableSource.getTableSource())) {
        final TableSource tableSource = (TableSource) secondaryTableSource.getTableSource();
        logicalTableName = database.toIdentifier(tableSource.getExplicitTableName());
        secondaryTable = namespace.locateTable(logicalTableName);
        if (secondaryTable == null) {
            secondaryTable = namespace.createTable(logicalTableName, false);
        } else {
            secondaryTable.setAbstract(false);
        }
        secondaryTable.setComment(tableSource.getComment());
    } else {
        final InLineViewSource inLineViewSource = (InLineViewSource) secondaryTableSource.getTableSource();
        secondaryTable = new Table(namespace, inLineViewSource.getSelectStatement(), false);
        logicalTableName = Identifier.toIdentifier(inLineViewSource.getLogicalName());
    }
    secondaryTableJoin.setTable(secondaryTable);
    entityTableXref.addSecondaryTable(mappingDocument, logicalTableName, secondaryTableJoin);
    bindCustomSql(mappingDocument, secondaryTableSource, secondaryTableJoin);
    secondaryTableJoin.setSequentialSelect(secondaryTableSource.getFetchStyle() == FetchStyle.SELECT);
    secondaryTableJoin.setInverse(secondaryTableSource.isInverse());
    secondaryTableJoin.setOptional(secondaryTableSource.isOptional());
    if (log.isDebugEnabled()) {
        log.debugf("Mapping entity secondary-table: %s -> %s", persistentClass.getEntityName(), secondaryTable.getName());
    }
    final SimpleValue keyBinding = new DependantValue(mappingDocument, secondaryTable, persistentClass.getIdentifier());
    if (mappingDocument.getBuildingOptions().useNationalizedCharacterData()) {
        keyBinding.makeNationalized();
    }
    secondaryTableJoin.setKey(keyBinding);
    keyBinding.setCascadeDeleteEnabled(secondaryTableSource.isCascadeDeleteEnabled());
    // NOTE : no Type info to bind...
    relationalObjectBinder.bindColumns(mappingDocument, secondaryTableSource.getPrimaryKeyColumnSources(), keyBinding, secondaryTableSource.isOptional(), new RelationalObjectBinder.ColumnNamingDelegate() {

        int count = 0;

        @Override
        public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
            final Column correspondingColumn = entityTableXref.getPrimaryTable().getPrimaryKey().getColumn(count++);
            return database.toIdentifier(correspondingColumn.getQuotedName());
        }
    });
    keyBinding.setForeignKeyName(secondaryTableSource.getExplicitForeignKeyName());
    // skip creating primary and foreign keys for a subselect.
    if (secondaryTable.getSubselect() == null) {
        secondaryTableJoin.createPrimaryKey();
        secondaryTableJoin.createForeignKey();
    }
}
Also used : Table(org.hibernate.mapping.Table) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) DependantValue(org.hibernate.mapping.DependantValue) InLineViewSource(org.hibernate.boot.model.source.spi.InLineViewSource) Namespace(org.hibernate.boot.model.relational.Namespace) SimpleValue(org.hibernate.mapping.SimpleValue) Identifier(org.hibernate.boot.model.naming.Identifier) TableSource(org.hibernate.boot.model.source.spi.TableSource) SecondaryTableSource(org.hibernate.boot.model.source.spi.SecondaryTableSource) Column(org.hibernate.mapping.Column) LocalMetadataBuildingContext(org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

Identifier (org.hibernate.boot.model.naming.Identifier)9 LocalMetadataBuildingContext (org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext)9 SimpleValue (org.hibernate.mapping.SimpleValue)7 MappingException (org.hibernate.boot.MappingException)4 Property (org.hibernate.mapping.Property)3 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)3 Column (org.hibernate.mapping.Column)2 DenormalizedTable (org.hibernate.mapping.DenormalizedTable)2 DependantValue (org.hibernate.mapping.DependantValue)2 Table (org.hibernate.mapping.Table)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ImplicitIdentifierColumnNameSource (org.hibernate.boot.model.naming.ImplicitIdentifierColumnNameSource)1 ImplicitIndexColumnNameSource (org.hibernate.boot.model.naming.ImplicitIndexColumnNameSource)1 ImplicitMapKeyColumnNameSource (org.hibernate.boot.model.naming.ImplicitMapKeyColumnNameSource)1 Namespace (org.hibernate.boot.model.relational.Namespace)1 IdentifierSourceSimple (org.hibernate.boot.model.source.spi.IdentifierSourceSimple)1 InLineViewSource (org.hibernate.boot.model.source.spi.InLineViewSource)1 PluralAttributeElementSourceManyToAny (org.hibernate.boot.model.source.spi.PluralAttributeElementSourceManyToAny)1 PluralAttributeElementSourceOneToMany (org.hibernate.boot.model.source.spi.PluralAttributeElementSourceOneToMany)1