Example usage for org.hibernate.mapping Table addColumn

List of usage examples for org.hibernate.mapping Table addColumn

Introduction

In this page you can find the example usage for org.hibernate.mapping Table addColumn.

Prototype

public void addColumn(Column column) 

Source Link

Usage

From source file:com.manydesigns.portofino.persistence.hibernate.HibernateConfig.java

License:Open Source License

protected Column createColumn(Mappings mappings, Table tab,
        com.manydesigns.portofino.model.database.Column column) {
    Column col = new Column();
    col.setName(escapeName(column.getColumnName()));
    col.setLength(column.getLength());//  www.  j  av  a2  s  .  c o  m
    col.setPrecision(column.getLength());
    col.setScale(column.getScale());
    col.setNullable(column.isNullable());
    String columnType = column.getColumnType();
    int jdbcType = column.getJdbcType();

    col.setSqlTypeCode(jdbcType);
    col.setSqlType(columnType);

    SimpleValue value = new SimpleValue(mappings, tab);
    if (!setHibernateType(value, column, column.getActualJavaType(), jdbcType)) {
        logger.error("Skipping column {}", column.getQualifiedName());
        return null;
    }

    value.addColumn(col);
    tab.addColumn(col);
    mappings.addColumnBinding(column.getColumnName(), col, tab);

    return col;
}

From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java

License:Apache License

/**
 * Patch the existing configuration.  These patches are 'additive' only.  Any columns
 * or tables found in the patch that do not already exist will be added.  Anything missing
 * will be ignored.//from  w  ww.  j  av a 2s.c om
 *
 * @param mapping the classpath reference to the patch hbm.xml file
 *
 * @throws IOException if there is a problem loading the patch file.
 */
public void patch(String mapping) throws IOException {
    // Ok, so we do this as follows:
    // a) load the patch mapping
    // b) apply it to the existing in memory mapping
    // c) sync the in memory mapping with the database.
    // Note: this falls under the same restrictions as normal hibernate syncs, so
    // any columns that change details will not need to be handled separately.  Only
    // column additions and table additions are included.

    MutableConfiguration config = new MutableConfiguration();
    config.addClassPathMappings(Arrays.asList(mapping));
    config.buildMappings();

    Iterator tables = config.getTableMappings();
    while (tables.hasNext()) {
        Table table = (Table) tables.next();

        // do we have this table?.
        Table existingTable = getTable(table.getName());
        if (existingTable != null) {
            Iterator columns = table.getColumnIterator();
            while (columns.hasNext()) {
                Column column = (Column) columns.next();

                Column existingColumn = getColumn(existingTable, column.getName());
                if (existingColumn == null) {
                    existingTable.addColumn(column);
                } else {
                    // avoid this case for now - we need to refresh the column with the new definition,
                    // which means updating the in memory column configuration, and then running a refresh.
                }
            }
        } else {
            this.config.addTable(table);
        }
    }

    sync();
}

From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java

License:Apache License

protected Table clone(Table table) {
    Table clone = new Table(table.getName());
    clone.setAbstract(table.isAbstract());
    clone.setCatalog(table.getCatalog());
    clone.setComment(table.getComment());
    clone.setName(table.getName());/* w  w w  .java  2s.co  m*/
    clone.setPrimaryKey(table.getPrimaryKey());
    clone.setQuoted(table.isQuoted());
    clone.setRowId(table.getRowId());
    clone.setSchema(table.getSchema());
    clone.setSubselect(table.getSubselect());

    Iterator columns = table.getColumnIterator();
    while (columns.hasNext()) {
        Column column = (Column) columns.next();
        clone.addColumn(column);
    }

    Iterator foreignKeys = table.getForeignKeyIterator();
    while (foreignKeys.hasNext()) {
        ForeignKey key = (ForeignKey) foreignKeys.next();
        clone.createForeignKey(key.getName(), key.getColumns(), key.getReferencedEntityName(),
                key.getReferencedColumns());
    }

    return clone;
}

From source file:com.zutubi.pulse.master.transfer.jdbc.HibernateUniqueKeyTable.java

License:Apache License

public static Table getMapping() {
    Table table = new Table("hibernate_unique_key");
    Column column = new Column("next_hi");
    SimpleValue value = new SimpleValue(null, table);
    value.setTypeName(int.class.getName());
    column.setValue(value);/*from  www .  jav  a2 s  .c  o m*/
    column.setSqlTypeCode(Types.INTEGER);
    table.addColumn(column);
    return table;
}

From source file:net.e6tech.elements.persist.hibernate.ModifiedTableGenerator.java

License:Apache License

@Override
public void registerExportables(Database database) {
    final Dialect dialect = database.getJdbcEnvironment().getDialect();

    final Namespace namespace = database.locateNamespace(qualifiedTableName.getCatalogName(),
            qualifiedTableName.getSchemaName());

    Table table = namespace.locateTable(qualifiedTableName.getObjectName());
    if (table == null) {
        table = namespace.createTable(qualifiedTableName.getObjectName(), false);

        // todo : note sure the best solution here.  do we add the columns if missing?  other?
        final Column segmentColumn = new ExportableColumn(database, table, segmentColumnName,
                StringType.INSTANCE, dialect.getTypeName(Types.VARCHAR, segmentValueLength, 0, 0));
        segmentColumn.setNullable(false);
        table.addColumn(segmentColumn);

        // lol/* w  w w.j ava  2  s  .c om*/
        table.setPrimaryKey(new PrimaryKey(table));
        table.getPrimaryKey().addColumn(segmentColumn);

        final Column valueColumn = new ExportableColumn(database, table, valueColumnName, LongType.INSTANCE);
        table.addColumn(valueColumn);
    }

    // allow physical naming strategies a chance to kick in
    this.renderedTableName = database.getJdbcEnvironment().getQualifiedObjectNameFormatter()
            .format(table.getQualifiedTableName(), dialect);

    this.selectQuery = buildSelectQuery(dialect);
    this.updateQuery = buildUpdateQuery();
    this.insertQuery = buildInsertQuery();

}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

protected void bindEnumType(GrailsDomainClassProperty property, Class<?> propertyType, SimpleValue simpleValue,
        String columnName) {//from www. j a  v  a2 s. c o m

    PropertyConfig pc = getPropertyConfig(property);
    String typeName = getTypeName(property, getPropertyConfig(property), getMapping(property.getDomainClass()));
    if (typeName == null) {
        Properties enumProperties = new Properties();
        enumProperties.put(ENUM_CLASS_PROP, propertyType.getName());

        String enumType = pc == null ? DEFAULT_ENUM_TYPE : pc.getEnumType();
        if (enumType.equals(DEFAULT_ENUM_TYPE) && identityEnumTypeSupports(propertyType)) {
            simpleValue.setTypeName("org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType");
        } else {
            simpleValue.setTypeName(ENUM_TYPE_CLASS);
            if (enumType.equals(DEFAULT_ENUM_TYPE) || "string".equalsIgnoreCase(enumType)) {
                enumProperties.put(ENUM_TYPE_PROP, String.valueOf(Types.VARCHAR));
            } else if (!"ordinal".equalsIgnoreCase(enumType)) {
                LOG.warn("Invalid enumType specified when mapping property [" + property.getName()
                        + "] of class [" + property.getDomainClass().getClazz().getName()
                        + "]. Using defaults instead.");
            }
        }
        simpleValue.setTypeParameters(enumProperties);
    } else {
        simpleValue.setTypeName(typeName);
    }

    Table t = simpleValue.getTable();
    Column column = new Column();

    if (property.getDomainClass().isRoot()) {
        column.setNullable(property.isOptional());
    } else {
        Mapping mapping = getMapping(property.getDomainClass());
        if (mapping == null || mapping.getTablePerHierarchy()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("[GrailsDomainBinder] Sub class property [" + property.getName()
                        + "] for column name [" + column.getName() + "] set to nullable");
            }
            column.setNullable(true);
        } else {
            column.setNullable(property.isOptional());
        }
    }
    column.setValue(simpleValue);
    column.setName(columnName);
    if (t != null)
        t.addColumn(column);

    simpleValue.addColumn(column);

    PropertyConfig propertyConfig = getPropertyConfig(property);
    if (propertyConfig != null && !propertyConfig.getColumns().isEmpty()) {
        bindIndex(columnName, column, propertyConfig.getColumns().get(0), t);
        bindColumnConfigToColumn(column, propertyConfig.getColumns().get(0));
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

protected void bindSimpleValue(GrailsDomainClassProperty grailsProp, GrailsDomainClassProperty parentProperty,
        SimpleValue simpleValue, String path, PropertyConfig propertyConfig, String sessionFactoryBeanName) {
    setTypeForPropertyConfig(grailsProp, simpleValue, propertyConfig);
    if (grailsProp.isDerived()) {
        Formula formula = new Formula();
        formula.setFormula(propertyConfig.getFormula());
        simpleValue.addFormula(formula);
    } else {/* ww w .  j ava2  s . co  m*/
        Table table = simpleValue.getTable();

        // Add the column definitions for this value/property. Note that
        // not all custom mapped properties will have column definitions,
        // in which case we still need to create a Hibernate column for
        // this value.
        List<?> columnDefinitions = propertyConfig != null ? propertyConfig.getColumns()
                : Arrays.asList(new Object[] { null });
        if (columnDefinitions.isEmpty()) {
            columnDefinitions = Arrays.asList(new Object[] { null });
        }

        for (int i = 0, n = columnDefinitions.size(); i < n; i++) {
            ColumnConfig cc = (ColumnConfig) columnDefinitions.get(i);
            Column column = new Column();

            // Check for explicitly mapped column name and SQL type.
            if (cc != null) {
                if (cc.getName() != null) {
                    column.setName(cc.getName());
                }
                if (cc.getSqlType() != null) {
                    column.setSqlType(cc.getSqlType());
                }
            }

            column.setValue(simpleValue);

            if (cc != null) {
                if (cc.getLength() != -1) {
                    column.setLength(cc.getLength());
                }
                if (cc.getPrecision() != -1) {
                    column.setPrecision(cc.getPrecision());
                }
                if (cc.getScale() != -1) {
                    column.setScale(cc.getScale());
                }
                column.setUnique(cc.isUnique());
            }

            bindColumn(grailsProp, parentProperty, column, cc, path, table, sessionFactoryBeanName);

            if (table != null) {
                table.addColumn(column);
            }

            simpleValue.addColumn(column);
        }
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

/**
 * Binds a value for the specified parameters to the meta model.
 *
 * @param type        The type of the property
 * @param simpleValue The simple value instance
 * @param nullable    Whether it is nullable
 * @param columnName  The property name//  w  ww .j a  v a2s  .  co  m
 * @param mappings    The mappings
 */
protected void bindSimpleValue(String type, SimpleValue simpleValue, boolean nullable, String columnName,
        Mappings mappings) {

    simpleValue.setTypeName(type);
    Table t = simpleValue.getTable();
    Column column = new Column();
    column.setNullable(nullable);
    column.setValue(simpleValue);
    column.setName(columnName);
    if (t != null)
        t.addColumn(column);

    simpleValue.addColumn(column);
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

private static void bindEnumType(GrailsDomainClassProperty property, Class<?> propertyType,
        SimpleValue simpleValue, String columnName) {
    Properties enumProperties = new Properties();
    enumProperties.put(ENUM_CLASS_PROP, propertyType.getName());

    PropertyConfig pc = getPropertyConfig(property);
    String enumType = pc != null ? pc.getEnumType() : DEFAULT_ENUM_TYPE;
    if (enumType.equals(DEFAULT_ENUM_TYPE) && IdentityEnumType.supports(propertyType)) {
        simpleValue.setTypeName(IdentityEnumType.class.getName());
    } else {//from   w  w  w. j  ava  2 s  .  c om
        simpleValue.setTypeName(ENUM_TYPE_CLASS);
        if (enumType.equals(DEFAULT_ENUM_TYPE) || "string".equalsIgnoreCase(enumType)) {
            enumProperties.put(ENUM_TYPE_PROP, String.valueOf(Types.VARCHAR));
        } else if (!"ordinal".equalsIgnoreCase(enumType)) {
            LOG.warn("Invalid enumType specified when mapping property [" + property.getName() + "] of class ["
                    + property.getDomainClass().getClazz().getName() + "]. Using defaults instead.");
        }
    }

    simpleValue.setTypeParameters(enumProperties);
    Table t = simpleValue.getTable();
    Column column = new Column();

    if (property.getDomainClass().isRoot()) {
        column.setNullable(property.isOptional());
    } else {
        Mapping mapping = getMapping(property.getDomainClass());
        if (mapping == null || mapping.getTablePerHierarchy()) {
            if (LOG.isDebugEnabled())
                LOG.debug("[GrailsDomainBinder] Sub class property [" + property.getName()
                        + "] for column name [" + column.getName() + "] set to nullable");
            column.setNullable(true);
        } else {
            column.setNullable(property.isOptional());
        }
    }
    column.setValue(simpleValue);
    column.setName(columnName);
    if (t != null)
        t.addColumn(column);

    simpleValue.addColumn(column);

    PropertyConfig propertyConfig = getPropertyConfig(property);
    if (propertyConfig != null && !propertyConfig.getColumns().isEmpty()) {
        bindIndex(columnName, column, propertyConfig.getColumns().get(0), t);
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

private static void bindSimpleValue(GrailsDomainClassProperty grailsProp,
        GrailsDomainClassProperty parentProperty, SimpleValue simpleValue, String path,
        PropertyConfig propertyConfig, String sessionFactoryBeanName) {
    setTypeForPropertyConfig(grailsProp, simpleValue, propertyConfig);
    if (grailsProp.isDerived()) {
        Formula formula = new Formula();
        formula.setFormula(propertyConfig.getFormula());
        simpleValue.addFormula(formula);
    } else {/* w  w  w  . java  2s .co m*/
        Table table = simpleValue.getTable();

        // Add the column definitions for this value/property. Note that
        // not all custom mapped properties will have column definitions,
        // in which case we still need to create a Hibernate column for
        // this value.
        List<?> columnDefinitions = propertyConfig != null ? propertyConfig.getColumns()
                : Arrays.asList(new Object[] { null });
        if (columnDefinitions.isEmpty()) {
            columnDefinitions = Arrays.asList(new Object[] { null });
        }

        for (int i = 0, n = columnDefinitions.size(); i < n; i++) {
            ColumnConfig cc = (ColumnConfig) columnDefinitions.get(i);
            Column column = new Column();

            // Check for explicitly mapped column name and SQL type.
            if (cc != null) {
                if (cc.getName() != null) {
                    column.setName(cc.getName());
                }
                if (cc.getSqlType() != null) {
                    column.setSqlType(cc.getSqlType());
                }
            }

            column.setValue(simpleValue);
            bindColumn(grailsProp, parentProperty, column, cc, path, table, sessionFactoryBeanName);

            if (cc != null) {
                if (cc.getLength() != -1) {
                    column.setLength(cc.getLength());
                }
                if (cc.getPrecision() != -1) {
                    column.setPrecision(cc.getPrecision());
                }
                if (cc.getScale() != -1) {
                    column.setScale(cc.getScale());
                }
                column.setUnique(cc.isUnique());
            }

            if (table != null)
                table.addColumn(column);

            simpleValue.addColumn(column);
        }
    }
}