Search in sources :

Example 31 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 32 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)

Example 33 with Identifier

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

the class ModelBinder method createBasicAttribute.

private Property createBasicAttribute(MappingDocument sourceDocument, final SingularAttributeSourceBasic attributeSource, SimpleValue value, String containingClassName) {
    final String attributeName = attributeSource.getName();
    bindSimpleValueType(sourceDocument, attributeSource.getTypeInformation(), value);
    relationalObjectBinder.bindColumnsAndFormulas(sourceDocument, attributeSource.getRelationalValueSources(), value, attributeSource.areValuesNullableByDefault(), new RelationalObjectBinder.ColumnNamingDelegate() {

        @Override
        public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
            return implicitNamingStrategy.determineBasicColumnName(attributeSource);
        }
    });
    prepareValueTypeViaReflection(sourceDocument, value, containingClassName, attributeName, attributeSource.getAttributeRole());
    //		// this is done here 'cos we might only know the type here (ugly!)
    //		// TODO: improve this a lot:
    //		if ( value instanceof ToOne ) {
    //			ToOne toOne = (ToOne) value;
    //			String propertyRef = toOne.getReferencedEntityAttributeName();
    //			if ( propertyRef != null ) {
    //				mappings.addUniquePropertyReference( toOne.getReferencedEntityName(), propertyRef );
    //			}
    //			toOne.setCascadeDeleteEnabled( "cascade".equals( subnode.attributeValue( "on-delete" ) ) );
    //		}
    //		else if ( value instanceof Collection ) {
    //			Collection coll = (Collection) value;
    //			String propertyRef = coll.getReferencedEntityAttributeName();
    //			// not necessarily a *unique* property reference
    //			if ( propertyRef != null ) {
    //				mappings.addPropertyReference( coll.getOwnerEntityName(), propertyRef );
    //			}
    //		}
    value.createForeignKey();
    Property property = new Property();
    property.setValue(value);
    bindProperty(sourceDocument, attributeSource, property);
    return property;
}
Also used : 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)

Example 34 with Identifier

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

the class Namespace method createDenormalizedTable.

public DenormalizedTable createDenormalizedTable(Identifier logicalTableName, boolean isAbstract, Table includedTable) {
    final Table existing = tables.get(logicalTableName);
    if (existing != null) {
        // for now assume it is
        return (DenormalizedTable) existing;
    }
    final Identifier physicalTableName = database.getPhysicalNamingStrategy().toPhysicalTableName(logicalTableName, database.getJdbcEnvironment());
    DenormalizedTable table = new DenormalizedTable(this, physicalTableName, isAbstract, includedTable);
    tables.put(logicalTableName, table);
    return table;
}
Also used : DenormalizedTable(org.hibernate.mapping.DenormalizedTable) Table(org.hibernate.mapping.Table) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) Identifier(org.hibernate.boot.model.naming.Identifier)

Example 35 with Identifier

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

the class Namespace method createSequence.

public Sequence createSequence(Identifier logicalName, int initialValue, int increment) {
    if (sequences.containsKey(logicalName)) {
        throw new HibernateException("Sequence was already registered with that name [" + logicalName.toString() + "]");
    }
    final Identifier physicalName = database.getPhysicalNamingStrategy().toPhysicalSequenceName(logicalName, database.getJdbcEnvironment());
    Sequence sequence = new Sequence(this.physicalName.getCatalog(), this.physicalName.getSchema(), physicalName, initialValue, increment);
    sequences.put(logicalName, sequence);
    return sequence;
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) HibernateException(org.hibernate.HibernateException)

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