Example usage for org.hibernate.mapping Column setName

List of usage examples for org.hibernate.mapping Column setName

Introduction

In this page you can find the example usage for org.hibernate.mapping Column setName.

Prototype

public void setName(String name) 

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  a  va2 s  .  co 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.manydesigns.portofino.persistence.hibernate.HibernateConfig.java

License:Open Source License

protected void createM2O(Configuration config, Mappings mappings, ForeignKey relationship) {
    com.manydesigns.portofino.model.database.Table manyMDTable = relationship.getFromTable();
    com.manydesigns.portofino.model.database.Table oneMDTable = relationship.getToTable();
    String manyMDQualifiedTableName = manyMDTable.getActualEntityName();
    String oneMDQualifiedTableName = oneMDTable.getActualEntityName();

    RootClass clazz = (RootClass) mappings.getClass(manyMDQualifiedTableName);
    if (clazz == null) {
        logger.error("Cannot find table '{}' as 'many' side of foreign key '{}'. Skipping relationship.",
                manyMDQualifiedTableName, relationship.getName());
        return;/*from w  w  w  .  j ava  2 s  . c  o  m*/
    }

    Table tab = clazz.getTable();
    List<String> columnNames = new ArrayList<String>();

    for (Reference ref : relationship.getReferences()) {
        if (ref.getActualFromColumn() == null) {
            logger.error("Missing from column {}, skipping relationship", ref.getFromColumn());
            return;
        }
        columnNames.add(ref.getFromColumn());
    }

    ManyToOne m2o = new ManyToOne(mappings, tab);
    m2o.setLazy(LAZY);
    final HashMap<String, PersistentClass> persistentClasses = new HashMap<String, PersistentClass>();
    persistentClasses.put(oneMDQualifiedTableName, config.getClassMapping(oneMDQualifiedTableName));
    m2o.setReferencedEntityName(oneMDQualifiedTableName);
    m2o.createPropertyRefConstraints(persistentClasses);

    PersistentClass manyClass = config.getClassMapping(manyMDQualifiedTableName);
    for (String columnName : columnNames) {
        Column col = new Column();
        col.setName(escapeName(columnName));
        //Recupero la colonna precedentemente associata alla tabella:
        //essa ha uno uniqueIdentifier generato al momento dell'associazione alla tabella;
        //questo viene utilizzato per disambiguare l'alias della colonna nelle query
        //SQL generate da Hibernate.
        col = manyClass.getTable().getColumn(col);
        if (col == null) {
            logger.error("Column not found in 'many' entity {}: {}, " + "skipping relationship",
                    manyClass.getEntityName(), columnName);
            return;
        }
        m2o.addColumn(col);
    }

    Property prop = new Property();
    prop.setName(relationship.getActualOnePropertyName());
    //prop.setNodeName(relationship.getActualOnePropertyName());
    prop.setValue(m2o);
    prop.setCascade("none"); //TODO era "all", capire
    prop.setInsertable(false);
    prop.setUpdateable(false);
    clazz.addProperty(prop);
}

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

License:Apache License

private void renameColumn(Connection connection, Table table, Column fromColumn, String toColumnName)
        throws SQLException {
    // a) identify foreign key references.
    List<ForeignKey> droppedConstraints = dropColumnConstraints(connection, table, fromColumn);

    // update table model.
    String fromColumnName = fromColumn.getName();
    fromColumn.setName(toColumnName);

    // add the new column to the table - synchronise the database with the updated schema.
    updateTableSchema(table, connection);

    // copy column data.
    sqlCopyColumn(connection, table.getName(), toColumnName, fromColumnName);

    // recreate the foreign key constraint if it exists.
    recreatedDroppedConstraints(droppedConstraints, connection);

    // d) drop the original column.
    sqlDropColumn(connection, table, fromColumnName);
}

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

License:Apache License

private void refreshColumn(Connection connection, Table table, Column column) throws SQLException {
    // create a temporary column. Make a temporary change to the in-memory schema to allow this.
    String columnName = column.getName();
    String tmpColumnName = "temporary_" + columnName;
    try {/*from   www . j av a 2  s .  c om*/
        column.setName(tmpColumnName);
        updateTableSchema(table, connection);
    } finally {
        column.setName(columnName);
    }

    // copy data to the temporary column.
    sqlCopyColumn(connection, table.getName(), tmpColumnName, columnName);

    // drop the existing column constraints.
    List<ForeignKey> droppedConstraints = dropColumnConstraints(connection, table, column);

    // drop original column.
    sqlDropColumn(connection, table, columnName);

    // recreate column - with the refreshed schema.
    updateTableSchema(table, connection);

    // copy the data back to the refreshed column.
    sqlCopyColumn(connection, table.getName(), columnName, tmpColumnName);

    // re-enable the foreign key constraints.
    recreatedDroppedConstraints(droppedConstraints, connection);

    // drop the temporary column.
    sqlDropColumn(connection, table, tmpColumnName);
}

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

License:Apache License

protected void linkValueUsingAColumnCopy(GrailsDomainClassProperty prop, Column column, DependantValue key) {
    Column mappingColumn = new Column();
    mappingColumn.setName(column.getName());
    mappingColumn.setLength(column.getLength());
    mappingColumn.setNullable(prop.isOptional());
    mappingColumn.setSqlType(column.getSqlType());

    mappingColumn.setValue(key);//from   w w w.j  ava 2  s. c  om
    key.addColumn(mappingColumn);
    key.getTable().addColumn(mappingColumn);
}

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

License:Apache License

/**
 * Creates and binds the discriminator property used in table-per-hierarchy inheritance to
 * discriminate between sub class instances
 *
 * @param table    The table to bind onto
 * @param entity   The root class entity
 * @param mappings The mappings instance
 *///  www  .  ja v  a2  s .co m
protected void bindDiscriminatorProperty(Table table, RootClass entity, Mappings mappings) {
    Mapping m = getMapping(entity.getMappedClass());
    SimpleValue d = new SimpleValue(mappings, table);
    entity.setDiscriminator(d);
    entity.setDiscriminatorValue(
            m != null && m.getDiscriminator() != null ? m.getDiscriminator() : entity.getClassName());

    if (m != null && m.getDiscriminatorMap().get("insert") != null) {
        entity.setDiscriminatorInsertable((Boolean) m.getDiscriminatorMap().get("insert"));
    }
    if (m != null && m.getDiscriminatorMap().get("type") != null) {
        d.setTypeName((String) m.getDiscriminatorMap().get("type"));
    }

    if (m != null && m.getDiscriminatorMap().get("formula") != null) {
        Formula formula = new Formula();
        formula.setFormula((String) m.getDiscriminatorMap().get("formula"));
        d.addFormula(formula);
    } else {
        bindSimpleValue(STRING_TYPE, d, false, RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME, mappings);

        ColumnConfig cc = m == null ? null : m.getDiscriminatorColumn();
        if (cc != null) {
            Column c = (Column) d.getColumnIterator().next();
            if (cc.getName() != null) {
                c.setName(cc.getName());
            }
            bindColumnConfigToColumn(c, cc);
        }
    }

    entity.setPolymorphic(true);
}

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  ww w. ja v  a 2  s  .  c  om

    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 {/* www .ja v a 2 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//ww  w  .  j a v a  2s .c  o  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.AbstractGrailsDomainBinder.java

License:Apache License

/**
 * Binds a Column instance to the Hibernate meta model
 *
 * @param property The Grails domain class property
 * @param parentProperty/*from   ww  w .j av a  2s . co  m*/
 * @param column     The column to bind
 * @param path
 * @param table      The table name
 * @param sessionFactoryBeanName  the session factory bean name
 */
protected void bindColumn(GrailsDomainClassProperty property, GrailsDomainClassProperty parentProperty,
        Column column, ColumnConfig cc, String path, Table table, String sessionFactoryBeanName) {

    if (cc != null) {
        column.setComment(cc.getComment());
        column.setDefaultValue(cc.getDefaultValue());
        column.setCustomRead(cc.getRead());
        column.setCustomWrite(cc.getWrite());
    }

    Class<?> userType = getUserType(property);
    String columnName = getColumnNameForPropertyAndPath(property, path, cc, sessionFactoryBeanName);
    if ((property.isAssociation() || property.isBasicCollectionType()) && userType == null) {
        // Only use conventional naming when the column has not been explicitly mapped.
        if (column.getName() == null) {
            column.setName(columnName);
        }
        if (property.isManyToMany()) {
            column.setNullable(false);
        } else if (property.isOneToOne() && property.isBidirectional() && !property.isOwningSide()) {
            if (property.getOtherSide().isHasOne()) {
                column.setNullable(false);
            } else {
                column.setNullable(true);
            }
        } else if ((property.isManyToOne() || property.isOneToOne()) && property.isCircular()) {
            column.setNullable(true);
        } else {
            column.setNullable(property.isOptional());
        }
    } else {
        column.setName(columnName);
        column.setNullable(property.isOptional() || (parentProperty != null && parentProperty.isOptional()));

        // Use the constraints for this property to more accurately define
        // the column's length, precision, and scale
        ConstrainedProperty constrainedProperty = getConstrainedProperty(property);
        if (constrainedProperty != null) {
            if (String.class.isAssignableFrom(property.getType())
                    || byte[].class.isAssignableFrom(property.getType())) {
                bindStringColumnConstraints(column, constrainedProperty);
            }

            if (Number.class.isAssignableFrom(property.getType())) {
                bindNumericColumnConstraints(column, constrainedProperty, cc);
            }
        }
    }

    handleUniqueConstraint(property, column, path, table, columnName, sessionFactoryBeanName);

    bindIndex(columnName, column, cc, table);

    if (!property.getDomainClass().isRoot()) {
        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());
        }
    }

    if (LOG.isDebugEnabled())
        LOG.debug("[GrailsDomainBinder] bound property [" + property.getName() + "] to column name ["
                + column.getName() + "] in table [" + table.getName() + "]");
}