Search in sources :

Example 26 with Identifier

use of org.hibernate.boot.model.naming.Identifier in project hibernate-orm by hibernate.

the class ModelBinder method bindMapKey.

private void bindMapKey(final MappingDocument mappingDocument, final IndexedPluralAttributeSource pluralAttributeSource, final org.hibernate.mapping.Map collectionBinding) {
    if (pluralAttributeSource.getIndexSource() instanceof PluralAttributeMapKeySourceBasic) {
        final PluralAttributeMapKeySourceBasic mapKeySource = (PluralAttributeMapKeySourceBasic) pluralAttributeSource.getIndexSource();
        final SimpleValue value = new SimpleValue(mappingDocument.getMetadataCollector(), collectionBinding.getCollectionTable());
        bindSimpleValueType(mappingDocument, mapKeySource.getTypeInformation(), value);
        if (!value.isTypeSpecified()) {
            throw new MappingException("map index element must specify a type: " + pluralAttributeSource.getAttributeRole().getFullPath(), mappingDocument.getOrigin());
        }
        relationalObjectBinder.bindColumnsAndFormulas(mappingDocument, mapKeySource.getRelationalValueSources(), value, true, new RelationalObjectBinder.ColumnNamingDelegate() {

            @Override
            public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
                return database.toIdentifier(IndexedCollection.DEFAULT_INDEX_COLUMN_NAME);
            }
        });
        collectionBinding.setIndex(value);
    } else if (pluralAttributeSource.getIndexSource() instanceof PluralAttributeMapKeySourceEmbedded) {
        final PluralAttributeMapKeySourceEmbedded mapKeySource = (PluralAttributeMapKeySourceEmbedded) pluralAttributeSource.getIndexSource();
        final Component componentBinding = new Component(mappingDocument.getMetadataCollector(), collectionBinding);
        bindComponent(mappingDocument, mapKeySource.getEmbeddableSource(), componentBinding, null, pluralAttributeSource.getName(), mapKeySource.getXmlNodeName(), false);
        collectionBinding.setIndex(componentBinding);
    } else if (pluralAttributeSource.getIndexSource() instanceof PluralAttributeMapKeyManyToManySource) {
        final PluralAttributeMapKeyManyToManySource mapKeySource = (PluralAttributeMapKeyManyToManySource) pluralAttributeSource.getIndexSource();
        final ManyToOne mapKeyBinding = new ManyToOne(mappingDocument.getMetadataCollector(), collectionBinding.getCollectionTable());
        mapKeyBinding.setReferencedEntityName(mapKeySource.getReferencedEntityName());
        relationalObjectBinder.bindColumnsAndFormulas(mappingDocument, mapKeySource.getRelationalValueSources(), mapKeyBinding, true, new RelationalObjectBinder.ColumnNamingDelegate() {

            @Override
            public Identifier determineImplicitName(final LocalMetadataBuildingContext context) {
                return implicitNamingStrategy.determineMapKeyColumnName(new ImplicitMapKeyColumnNameSource() {

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

                    @Override
                    public MetadataBuildingContext getBuildingContext() {
                        return context;
                    }
                });
            }
        });
        collectionBinding.setIndex(mapKeyBinding);
    } else if (pluralAttributeSource.getIndexSource() instanceof PluralAttributeMapKeyManyToAnySource) {
        final PluralAttributeMapKeyManyToAnySource mapKeySource = (PluralAttributeMapKeyManyToAnySource) pluralAttributeSource.getIndexSource();
        final Any mapKeyBinding = new Any(mappingDocument.getMetadataCollector(), collectionBinding.getCollectionTable());
        bindAny(mappingDocument, mapKeySource, mapKeyBinding, pluralAttributeSource.getAttributeRole().append("key"), pluralAttributeSource.getAttributePath().append("key"));
        collectionBinding.setIndex(mapKeyBinding);
    }
}
Also used : PluralAttributeMapKeyManyToAnySource(org.hibernate.boot.model.source.spi.PluralAttributeMapKeyManyToAnySource) PluralAttributeElementSourceManyToAny(org.hibernate.boot.model.source.spi.PluralAttributeElementSourceManyToAny) Any(org.hibernate.mapping.Any) SingularAttributeSourceAny(org.hibernate.boot.model.source.spi.SingularAttributeSourceAny) SingularAttributeSourceManyToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceManyToOne) ManyToOne(org.hibernate.mapping.ManyToOne) SimpleValue(org.hibernate.mapping.SimpleValue) MappingException(org.hibernate.boot.MappingException) Identifier(org.hibernate.boot.model.naming.Identifier) PluralAttributeMapKeySourceEmbedded(org.hibernate.boot.model.source.spi.PluralAttributeMapKeySourceEmbedded) PluralAttributeMapKeyManyToManySource(org.hibernate.boot.model.source.spi.PluralAttributeMapKeyManyToManySource) LocalMetadataBuildingContext(org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext) Component(org.hibernate.mapping.Component) ImplicitMapKeyColumnNameSource(org.hibernate.boot.model.naming.ImplicitMapKeyColumnNameSource) PluralAttributeMapKeySourceBasic(org.hibernate.boot.model.source.spi.PluralAttributeMapKeySourceBasic)

Example 27 with Identifier

use of org.hibernate.boot.model.naming.Identifier in project hibernate-orm by hibernate.

the class ModelBinder method bindEntityVersion.

private void bindEntityVersion(MappingDocument sourceDocument, EntityHierarchySourceImpl hierarchySource, RootClass rootEntityDescriptor) {
    final VersionAttributeSource versionAttributeSource = hierarchySource.getVersionAttributeSource();
    final SimpleValue versionValue = new SimpleValue(sourceDocument.getMetadataCollector(), rootEntityDescriptor.getTable());
    versionValue.makeVersion();
    bindSimpleValueType(sourceDocument, versionAttributeSource.getTypeInformation(), versionValue);
    relationalObjectBinder.bindColumnsAndFormulas(sourceDocument, versionAttributeSource.getRelationalValueSources(), versionValue, false, new RelationalObjectBinder.ColumnNamingDelegate() {

        @Override
        public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
            return implicitNamingStrategy.determineBasicColumnName(versionAttributeSource);
        }
    });
    Property prop = new Property();
    prop.setValue(versionValue);
    bindProperty(sourceDocument, versionAttributeSource, prop);
    // but just to make sure...
    if (prop.getValueGenerationStrategy() != null) {
        if (prop.getValueGenerationStrategy().getGenerationTiming() == GenerationTiming.INSERT) {
            throw new MappingException("'generated' attribute cannot be 'insert' for version/timestamp property", sourceDocument.getOrigin());
        }
    }
    if (versionAttributeSource.getUnsavedValue() != null) {
        versionValue.setNullValue(versionAttributeSource.getUnsavedValue());
    } else {
        versionValue.setNullValue("undefined");
    }
    rootEntityDescriptor.setVersion(prop);
    rootEntityDescriptor.setDeclaredVersion(prop);
    rootEntityDescriptor.addProperty(prop);
}
Also used : VersionAttributeSource(org.hibernate.boot.model.source.spi.VersionAttributeSource) Identifier(org.hibernate.boot.model.naming.Identifier) 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 28 with Identifier

use of org.hibernate.boot.model.naming.Identifier in project hibernate-orm by hibernate.

the class ModelBinder method bindJoinedSubclassEntity.

private void bindJoinedSubclassEntity(JoinedSubclassEntitySourceImpl entitySource, JoinedSubclass entityDescriptor) {
    MappingDocument mappingDocument = entitySource.sourceMappingDocument();
    bindBasicEntityValues(mappingDocument, entitySource, entityDescriptor);
    final Table primaryTable = bindEntityTableSpecification(mappingDocument, entitySource.getPrimaryTable(), null, entitySource, entityDescriptor);
    entityDescriptor.setTable(primaryTable);
    if (log.isDebugEnabled()) {
        log.debugf("Mapping joined-subclass: %s -> %s", entityDescriptor.getEntityName(), primaryTable.getName());
    }
    // KEY
    final SimpleValue keyBinding = new DependantValue(mappingDocument.getMetadataCollector(), primaryTable, entityDescriptor.getIdentifier());
    if (mappingDocument.getBuildingOptions().useNationalizedCharacterData()) {
        keyBinding.makeNationalized();
    }
    entityDescriptor.setKey(keyBinding);
    keyBinding.setCascadeDeleteEnabled(entitySource.isCascadeDeleteEnabled());
    relationalObjectBinder.bindColumns(mappingDocument, entitySource.getPrimaryKeyColumnSources(), keyBinding, false, new RelationalObjectBinder.ColumnNamingDelegate() {

        int count = 0;

        @Override
        public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
            final Column column = primaryTable.getPrimaryKey().getColumn(count++);
            return database.toIdentifier(column.getQuotedName());
        }
    });
    keyBinding.setForeignKeyName(entitySource.getExplicitForeignKeyName());
    // model.getKey().setType( new Type( model.getIdentifier() ) );
    entityDescriptor.createPrimaryKey();
    entityDescriptor.createForeignKey();
    // todo : tooling hints
    bindAllEntityAttributes(entitySource.sourceMappingDocument(), entitySource, entityDescriptor);
    bindJoinedSubclassEntities(entitySource, entityDescriptor);
}
Also used : Table(org.hibernate.mapping.Table) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) Identifier(org.hibernate.boot.model.naming.Identifier) DependantValue(org.hibernate.mapping.DependantValue) Column(org.hibernate.mapping.Column) LocalMetadataBuildingContext(org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext) SimpleValue(org.hibernate.mapping.SimpleValue)

Example 29 with Identifier

use of org.hibernate.boot.model.naming.Identifier in project hibernate-orm by hibernate.

the class RelationalObjectBinder method bindColumn.

public void bindColumn(MappingDocument sourceDocument, ColumnSource columnSource, SimpleValue simpleValue, boolean areColumnsNullableByDefault, ColumnNamingDelegate columnNamingDelegate) {
    Table table = simpleValue.getTable();
    final Column column = new Column();
    column.setValue(simpleValue);
    // resolve column name
    final Identifier logicalName;
    if (StringHelper.isNotEmpty(columnSource.getName())) {
        logicalName = database.toIdentifier(columnSource.getName());
    } else {
        logicalName = columnNamingDelegate.determineImplicitName(sourceDocument);
    }
    final Identifier physicalName = physicalNamingStrategy.toPhysicalColumnName(logicalName, database.getJdbcEnvironment());
    column.setName(physicalName.render(database.getDialect()));
    if (table != null) {
        table.addColumn(column);
        sourceDocument.getMetadataCollector().addColumnNameBinding(table, logicalName, column);
    }
    if (columnSource.getSizeSource() != null) {
        // to indicate null
        if (columnSource.getSizeSource().getLength() != null) {
            column.setLength(columnSource.getSizeSource().getLength());
        } else {
            column.setLength(Column.DEFAULT_LENGTH);
        }
        if (columnSource.getSizeSource().getScale() != null) {
            column.setScale(columnSource.getSizeSource().getScale());
        } else {
            column.setScale(Column.DEFAULT_SCALE);
        }
        if (columnSource.getSizeSource().getPrecision() != null) {
            column.setPrecision(columnSource.getSizeSource().getPrecision());
        } else {
            column.setPrecision(Column.DEFAULT_PRECISION);
        }
    }
    column.setNullable(interpretNullability(columnSource.isNullable(), areColumnsNullableByDefault));
    column.setUnique(columnSource.isUnique());
    column.setCheckConstraint(columnSource.getCheckCondition());
    column.setDefaultValue(columnSource.getDefaultValue());
    column.setSqlType(columnSource.getSqlType());
    column.setComment(columnSource.getComment());
    column.setCustomRead(columnSource.getReadFragment());
    column.setCustomWrite(columnSource.getWriteFragment());
    simpleValue.addColumn(column);
    if (table != null) {
        for (String name : columnSource.getIndexConstraintNames()) {
            table.getOrCreateIndex(name).addColumn(column);
        }
        for (String name : columnSource.getUniqueKeyConstraintNames()) {
            table.getOrCreateUniqueKey(name).addColumn(column);
        }
    }
}
Also used : Table(org.hibernate.mapping.Table) Identifier(org.hibernate.boot.model.naming.Identifier) Column(org.hibernate.mapping.Column)

Example 30 with Identifier

use of org.hibernate.boot.model.naming.Identifier in project hibernate-orm by hibernate.

the class ModelBinder method determineTable.

private Identifier determineTable(MappingDocument mappingDocument, SingularAttributeSourceEmbedded embeddedAttributeSource) {
    Identifier tableName = null;
    for (AttributeSource attributeSource : embeddedAttributeSource.getEmbeddableSource().attributeSources()) {
        final Identifier determinedName;
        if (RelationalValueSourceContainer.class.isInstance(attributeSource)) {
            determinedName = determineTable(mappingDocument, embeddedAttributeSource.getAttributeRole().getFullPath(), (RelationalValueSourceContainer) attributeSource);
        } else if (SingularAttributeSourceEmbedded.class.isInstance(attributeSource)) {
            determinedName = determineTable(mappingDocument, (SingularAttributeSourceEmbedded) attributeSource);
        } else if (SingularAttributeSourceAny.class.isInstance(attributeSource)) {
            determinedName = determineTable(mappingDocument, attributeSource.getAttributeRole().getFullPath(), ((SingularAttributeSourceAny) attributeSource).getKeySource().getRelationalValueSources());
        } else {
            continue;
        }
        if (EqualsHelper.equals(tableName, determinedName)) {
            continue;
        }
        if (tableName != null) {
            throw new MappingException(String.format(Locale.ENGLISH, "Attribute [%s] referenced columns from multiple tables: %s, %s", embeddedAttributeSource.getAttributeRole().getFullPath(), tableName, determinedName), mappingDocument.getOrigin());
        }
        tableName = determinedName;
    }
    return tableName;
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) VersionAttributeSource(org.hibernate.boot.model.source.spi.VersionAttributeSource) AttributeSource(org.hibernate.boot.model.source.spi.AttributeSource) PluralAttributeSource(org.hibernate.boot.model.source.spi.PluralAttributeSource) SingularAttributeSource(org.hibernate.boot.model.source.spi.SingularAttributeSource) RelationalValueSourceContainer(org.hibernate.boot.model.source.spi.RelationalValueSourceContainer) SingularAttributeSourceEmbedded(org.hibernate.boot.model.source.spi.SingularAttributeSourceEmbedded) MappingException(org.hibernate.boot.MappingException)

Aggregations

Identifier (org.hibernate.boot.model.naming.Identifier)46 Table (org.hibernate.mapping.Table)15 LocalMetadataBuildingContext (org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext)10 DenormalizedTable (org.hibernate.mapping.DenormalizedTable)9 SimpleValue (org.hibernate.mapping.SimpleValue)9 HashMap (java.util.HashMap)7 MappingException (org.hibernate.boot.MappingException)7 Namespace (org.hibernate.boot.model.relational.Namespace)7 Database (org.hibernate.boot.model.relational.Database)6 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 ArrayList (java.util.ArrayList)5 DatabaseIdentifier (org.hibernate.boot.model.naming.DatabaseIdentifier)5 Column (org.hibernate.mapping.Column)5 Property (org.hibernate.mapping.Property)5 HashSet (java.util.HashSet)4 DuplicateMappingException (org.hibernate.DuplicateMappingException)4 ImplicitNamingStrategy (org.hibernate.boot.model.naming.ImplicitNamingStrategy)4 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)4 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)4