Example usage for org.hibernate.mapping Column setLength

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

Introduction

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

Prototype

public void setLength(int length) 

Source Link

Usage

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 {/*  www.j  av a  2  s  .c  om*/
        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);
        }
    }
}

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

License:Apache License

/**
 * Interrogates the specified constraints looking for any constraints that would limit the
 * length of the property's value.  If such constraints exist, this method adjusts the length
 * of the column accordingly./*from w w w. j av a  2 s.  co  m*/
 *
 * @param column              the column that corresponds to the property
 * @param constrainedProperty the property's constraints
 */
protected static void bindStringColumnConstraints(Column column, ConstrainedProperty constrainedProperty) {
    Integer columnLength = constrainedProperty.getMaxSize();
    List<?> inListValues = constrainedProperty.getInList();
    if (columnLength != null) {
        column.setLength(columnLength.intValue());
    } else if (inListValues != null) {
        column.setLength(getMaxSize(inListValues));
    }
}

From source file:org.eclipse.emf.teneo.hibernate.HbDataStore.java

License:Open Source License

protected void addContainerMappingToPC(PersistentClass pc) {
    if (log.isDebugEnabled()) {
        log.debug("Adding eContainer and econtainerfeatureid properties to " + pc.getClassName());
    }//from w ww  . j a v a 2s  .co  m
    final EContainerFeaturePersistenceStrategy featurePersistenceStrategy = getPersistenceOptions()
            .getEContainerFeaturePersistenceStrategy();

    final Property eContainer = new Property();
    eContainer.setName(HbConstants.PROPERTY_ECONTAINER);
    eContainer.setMetaAttributes(new HashMap<Object, Object>());
    eContainer.setNodeName(eContainer.getName());
    eContainer.setPropertyAccessorName(EContainerAccessor.class.getName());

    final SimpleValue sv = new SimpleValue(getMappings(), pc.getTable());
    sv.setTypeName(EContainerUserType.class.getName());

    final Column eccColumn = new Column(getPersistenceOptions().getSQLColumnNamePrefix()
            + getPersistenceOptions().getEContainerClassColumn());
    sv.addColumn(checkColumnExists(pc.getTable(), eccColumn));

    final Column ecColumn = new Column(
            getPersistenceOptions().getSQLColumnNamePrefix() + getPersistenceOptions().getEContainerColumn());
    sv.addColumn(checkColumnExists(pc.getTable(), ecColumn));

    eContainer.setValue(sv);
    pc.addProperty(eContainer);

    if (featurePersistenceStrategy.equals(EContainerFeaturePersistenceStrategy.FEATUREID)
            || featurePersistenceStrategy.equals(EContainerFeaturePersistenceStrategy.BOTH)) {
        final Property ecFID = new Property();
        ecFID.setName(HbConstants.PROPERTY_ECONTAINER_FEATURE_ID);
        ecFID.setMetaAttributes(new HashMap<Object, Object>());
        ecFID.setNodeName(ecFID.getName());
        ecFID.setPropertyAccessorName(EContainerFeatureIDAccessor.class.getName());
        final SimpleValue svfid = new SimpleValue(getMappings(), pc.getTable());
        svfid.setTypeName("integer");

        final Column ecfColumn = new Column(
                getPersistenceOptions().getSQLColumnNamePrefix() + HbConstants.COLUMN_ECONTAINER_FEATUREID);
        svfid.addColumn(checkColumnExists(pc.getTable(), ecfColumn));

        ecFID.setValue(svfid);
        pc.addProperty(ecFID);
    }
    if (featurePersistenceStrategy.equals(EContainerFeaturePersistenceStrategy.FEATURENAME)
            || featurePersistenceStrategy.equals(EContainerFeaturePersistenceStrategy.BOTH)) {
        final Property ecFID = new Property();
        ecFID.setName(HbConstants.PROPERTY_ECONTAINER_FEATURE_NAME);
        ecFID.setMetaAttributes(new HashMap<Object, Object>());
        ecFID.setNodeName(ecFID.getName());
        ecFID.setPropertyAccessorName(NewEContainerFeatureIDPropertyHandler.class.getName());
        final SimpleValue svfid = new SimpleValue(getMappings(), pc.getTable());
        svfid.setTypeName(EContainerFeatureIDUserType.class.getName());

        final Column ecfColumn = new Column(getPersistenceOptions().getSQLColumnNamePrefix()
                + getPersistenceOptions().getEContainerFeatureNameColumn());

        ecfColumn.setLength(getEContainerFeatureNameColumnLength());

        svfid.addColumn(checkColumnExists(pc.getTable(), ecfColumn));

        ecFID.setValue(svfid);
        pc.addProperty(ecFID);
    }
}

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

License:Apache License

protected void bindColumnConfigToColumn(PersistentProperty property, Column column, ColumnConfig columnConfig) {
    final PropertyConfig mappedForm = property != null ? (PropertyConfig) property.getMapping().getMappedForm()
            : null;//from w  ww.  j a v  a 2s  .  c  o  m
    boolean allowUnique = mappedForm != null && !mappedForm.isUniqueWithinGroup();

    if (columnConfig == null) {
        return;
    }

    if (columnConfig.getLength() != -1) {
        column.setLength(columnConfig.getLength());
    }
    if (columnConfig.getPrecision() != -1) {
        column.setPrecision(columnConfig.getPrecision());
    }
    if (columnConfig.getScale() != -1) {
        column.setScale(columnConfig.getScale());
    }
    if (columnConfig.getSqlType() != null && !columnConfig.getSqlType().isEmpty()) {
        column.setSqlType(columnConfig.getSqlType());
    }

    if (allowUnique) {
        column.setUnique(columnConfig.getUnique());
    }
}

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

License:Apache License

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

    mappingColumn.setValue(key);/*  w  w w.  j a v a 2  s  .c  o  m*/
    key.addColumn(mappingColumn);
    key.getTable().addColumn(mappingColumn);
}

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

License:Apache License

protected void bindSimpleValue(PersistentProperty grailsProp, PersistentProperty parentProperty,
        SimpleValue simpleValue, String path, PropertyConfig propertyConfig, String sessionFactoryBeanName) {
    setTypeForPropertyConfig(grailsProp, simpleValue, propertyConfig);
    final PropertyConfig mappedForm = (PropertyConfig) grailsProp.getMapping().getMappedForm();
    if (mappedForm.isDerived()) {
        Formula formula = new Formula();
        formula.setFormula(propertyConfig.getFormula());
        simpleValue.addFormula(formula);
    } else {/*from w  w w  .  j  a 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 (Object columnDefinition : columnDefinitions) {
            ColumnConfig cc = (ColumnConfig) columnDefinition;
            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());
                }
                if (!mappedForm.isUniqueWithinGroup()) {
                    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.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

/**
 * Interrogates the specified constraints looking for any constraints that would limit the
 * length of the property's value.  If such constraints exist, this method adjusts the length
 * of the column accordingly./*from  w ww  .  j a v a2  s .  co  m*/
 *  @param column              the column that corresponds to the property
 * @param constrainedProperty the property's constraints
 */
protected void bindStringColumnConstraints(Column column, PersistentProperty constrainedProperty) {
    final org.grails.datastore.mapping.config.Property mappedForm = constrainedProperty.getMapping()
            .getMappedForm();
    Number columnLength = mappedForm.getMaxSize();
    List<?> inListValues = mappedForm.getInList();
    if (columnLength != null) {
        column.setLength(columnLength.intValue());
    } else if (inListValues != null) {
        column.setLength(getMaxSize(inListValues));
    }
}

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

License:Apache License

protected void bindColumnConfigToColumn(PersistentProperty property, Column column, ColumnConfig columnConfig) {
    final PropertyConfig mappedForm = property != null ? (PropertyConfig) property.getMapping().getMappedForm()
            : null;//w  ww .j  a  v a 2 s  . c o  m
    boolean allowUnique = mappedForm != null && !mappedForm.isUniqueWithinGroup();

    if (columnConfig == null) {
        return;
    }

    if (columnConfig.getLength() != -1) {
        column.setLength(columnConfig.getLength());
    }
    if (columnConfig.getPrecision() != -1) {
        column.setPrecision(columnConfig.getPrecision());
    }
    if (columnConfig.getScale() != -1) {
        column.setScale(columnConfig.getScale());
    }
    if (columnConfig.getSqlType() != null && !columnConfig.getSqlType().isEmpty()) {
        column.setSqlType(columnConfig.getSqlType());
    }
    if (allowUnique) {
        column.setUnique(columnConfig.getUnique());
    }
}

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

License:Apache License

protected void bindSimpleValue(PersistentProperty grailsProp, PersistentProperty parentProperty,
        SimpleValue simpleValue, String path, PropertyConfig propertyConfig, String sessionFactoryBeanName) {
    setTypeForPropertyConfig(grailsProp, simpleValue, propertyConfig);
    final PropertyConfig mappedForm = (PropertyConfig) grailsProp.getMapping().getMappedForm();
    if (mappedForm.isDerived() && !(grailsProp instanceof TenantId)) {
        Formula formula = new Formula();
        formula.setFormula(propertyConfig.getFormula());
        simpleValue.addFormula(formula);
    } else {//from  w  ww  .  j  a  va2 s.c  o m
        Table table = simpleValue.getTable();
        boolean hasConfig = propertyConfig != null;

        String generator = hasConfig ? propertyConfig.getGenerator() : null;
        if (generator != null) {
            simpleValue.setIdentifierGeneratorStrategy(generator);
            Properties params = propertyConfig.getTypeParams();
            if (params != null) {
                Properties generatorProps = new Properties();
                generatorProps.putAll(params);

                if (generatorProps.containsKey(SEQUENCE_KEY)) {
                    generatorProps.put(SequenceStyleGenerator.SEQUENCE_PARAM,
                            generatorProps.getProperty(SEQUENCE_KEY));
                }
                simpleValue.setIdentifierGeneratorProperties(generatorProps);
            }
        }

        // 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 = hasConfig ? propertyConfig.getColumns()
                : Arrays.asList(new Object[] { null });
        if (columnDefinitions.isEmpty()) {
            columnDefinitions = Arrays.asList(new Object[] { null });
        }

        for (Object columnDefinition : columnDefinitions) {
            ColumnConfig cc = (ColumnConfig) columnDefinition;
            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());
                }
                if (!mappedForm.isUniqueWithinGroup()) {
                    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.openflexo.technologyadapter.jdbc.model.DynamicModelBuilder.java

License:Open Source License

public Metadata buildDynamicModel() {
    Metadata metadata = metadataCollector.buildMetadataInstance(metadataBuildingContext);

    Database database = metadata.getDatabase();

    // **********
    // Creation / Dfinition de la table T_Dynamic_Table

    Table table = metadataCollector.addTable("", "", "T_Dynamic_Table", null, false);
    table.setName("T_Dynamic_Table");
    Column col = new Column();
    col.setName("pouet");
    col.setLength(256);
    col.setSqlType("CHAR(256)");
    col.setNullable(false);/*from  ww  w.j a  va2  s  .c om*/
    table.addColumn(col);

    PrimaryKey pk = new PrimaryKey(table);
    pk.addColumn(col);

    UniqueKey uk1 = new UniqueKey();
    uk1.setName("Nom_Unique");
    uk1.setTable(table);
    uk1.addColumn(col);
    table.addUniqueKey(uk1);

    Column col2 = new Column();
    col2.setName("padam");
    col2.setLength(256);
    col2.setSqlType("CHAR(256)");
    col2.setNullable(true);
    table.addColumn(col2);
    // pour rire les couples "Nom + Prenom" doivent tre uniques
    UniqueKey uk = new UniqueKey();
    uk.setName("Couple_Nom_Prenom_Unique");
    uk.setTable(table);
    uk.addColumn(col);
    uk.addColumn(col2);
    table.addUniqueKey(uk);

    // une colonne de clef etrangre vers T_Adresse
    Column col3 = new Column();
    col3.setName("id_addr");
    col3.setLength(16);
    col3.setSqlType("INTEGER");
    col3.setNullable(true);
    table.addColumn(col3);

    // **********
    // Creation / Dfinition de la table T_Adresse
    Table table2 = metadataCollector.addTable("", "", "T_Adresse", null, false);
    table2.setName("T_Adresse");
    Column col4 = new Column();
    col4.setName("Id");
    col4.setLength(16);
    col4.setSqlType("INTEGER");
    col4.setNullable(false);
    table2.addColumn(col4);

    pk = new PrimaryKey(table2);
    pk.addColumn(col);

    uk1 = new UniqueKey();
    uk1.setName("Id_Unique");
    uk1.setTable(table2);
    uk1.addColumn(col4);
    table.addUniqueKey(uk1);

    Column col5 = new Column();
    col5.setName("Adresse");
    col5.setLength(512);
    col5.setSqlType("CHAR(512)");
    col5.setNullable(true);
    table2.addColumn(col5);

    // ************************
    // Creation de l'entit persiste "Dynamic_Class"

    RootClass pClass = new RootClass(metadataBuildingContext);
    pClass.setEntityName("Dynamic_Class");
    pClass.setJpaEntityName("Dynamic_Class");
    pClass.setTable(table);
    metadataCollector.addEntityBinding(pClass);

    // Creation d'une proprit (clef) et son mapping

    Property prop = new Property();
    prop.setName("Nom");
    SimpleValue value = new SimpleValue((MetadataImplementor) metadata, table);
    value.setTypeName("java.lang.String");
    value.setIdentifierGeneratorStrategy("assigned");
    value.addColumn(col);
    value.setTable(table);
    prop.setValue(value);
    pClass.setDeclaredIdentifierProperty(prop);
    pClass.setIdentifierProperty(prop);
    pClass.setIdentifier(value);

    // Creation d'une proprit et son mapping

    prop = new Property();
    prop.setName("Prenom");
    value = new SimpleValue((MetadataImplementor) metadata, table);
    value.setTypeName(String.class.getCanonicalName());
    value.addColumn(col2);
    value.setTable(table);
    prop.setValue(value);
    pClass.addProperty(prop);

    // ************************
    // Creation de l'entit persiste "Adresse"

    RootClass pClass2 = new RootClass(metadataBuildingContext);
    pClass2.setEntityName("Adresse");
    pClass2.setJpaEntityName("Adresse");
    pClass2.setTable(table2);
    metadataCollector.addEntityBinding(pClass2);

    // Creation d'une proprit (clef) et son mapping

    prop = new Property();
    prop.setName("Identifiant");
    value = new SimpleValue((MetadataImplementor) metadata, table2);
    value.setTypeName("java.lang.Integer");
    value.setIdentifierGeneratorStrategy("native");
    value.addColumn(col4);
    value.setTable(table2);
    prop.setValue(value);
    pClass2.setDeclaredIdentifierProperty(prop);
    pClass2.setIdentifierProperty(prop);
    pClass2.setIdentifier(value);

    // Creation d'une proprit et son mapping

    prop = new Property();
    prop.setName("Prenom");
    value = new SimpleValue((MetadataImplementor) metadata, table2);
    value.setTypeName(String.class.getCanonicalName());
    value.addColumn(col5);
    value.setTable(table2);
    prop.setValue(value);
    pClass2.addProperty(prop);

    try {
        ((MetadataImplementor) metadata).validate();
    } catch (MappingException e) {
        System.out.println("Validation Error: " + e.getMessage());
    }

    return metadata;

}