Example usage for org.hibernate.mapping Column setSqlType

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

Introduction

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

Prototype

public void setSqlType(String sqlType) 

Source Link

Usage

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;/* ww w  .j a v  a 2 s.co 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 av a  2 s .  c  om*/
        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);/*from w ww  .j a  va  2 s. c o  m*/
    col.setSqlType("CHAR(256)");
    col.setNullable(false);
    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;

}

From source file:org.ow2.bonita.util.DbTool.java

License:Open Source License

@SuppressWarnings("unchecked")
public static void updateDatabaseSchema(Configuration configuration) {
    if (isOnDb("mysql", configuration)) {
        LOG.severe("Running on MySQL database, updating schema...");
        final PersistentClass pc = configuration.getClassMapping(Lob.class.getName());
        final Table table = pc.getTable();
        final Iterator<Column> columns = (Iterator<Column>) table.getColumnIterator();
        while (columns.hasNext()) {
            final Column column = columns.next();
            final String columnName = "BLOB_VALUE_";
            if (column.getName().equals(columnName)) {
                LOG.severe("Updating " + columnName + " column...");
                column.setSqlType("LONGBLOB");
                column.setLength(518576);
            }//from  w  w w. j  ava  2 s  .c  o m
        }
    } else if (DbTool.isOnDb("oracle", configuration)) {
        LOG.severe("Running on Oracle database, updating schema...");
        final Iterator<Table> tables = (Iterator<Table>) configuration.getTableMappings();
        while (tables.hasNext()) {
            final Table table = tables.next();
            final Iterator<Column> columns = (Iterator<Column>) table.getColumnIterator();
            while (columns.hasNext()) {
                final Column column = columns.next();
                final Value value = column.getValue();
                // Prevent ORA-01754: a table may contain only one column of type LONG
                if (value.getType() instanceof TextType) {
                    column.setSqlType("CLOB");
                }
            }
        }
    } /*else if (isOnDb("sybase", config)) {
      LOG.severe("Running on Sybase DB, updating schema...");
      //iterate over all tables and all columns to replace type=text
      final Iterator<Table> tables = (Iterator<Table>) config.getTableMappings();
      while (tables.hasNext()) {
        final Table table = tables.next();
        final Iterator<Column> columns = table.getColumnIterator();
        while (columns.hasNext()) {
          final Column column = columns.next();
          System.err.println("Column.name=" + column.getName() + ", column=" + column.getDefaultValue());
          if (!column.getName().equals("BLOB_VALUE_") && column.getSqlType() != null && column.getSqlType().equals("CLOB")) {
      LOG.severe("Updating " + column.getName() + " column...");
      column.setSqlType("LONGVARCHAR");
          }
        }
                
      }
      } */
}

From source file:org.processbase.engine.bam.db.ScriptGenerator.java

License:Open Source License

public ArrayList<String> getCreateTableScript(MetaKpi metaKpi, int type) {

    try {//from   w  w  w.j a  v a2s  . c  om
        Configuration config = new Configuration();

        Dialect d = (Dialect) Class.forName(BAMConstants.BAM_DB_DIALECT).newInstance();

        Mappings mappings = config.createMappings();
        table = mappings.addTable(null, null, metaKpi.getCode(), null, false);

        Column id = new Column("ID");
        SimpleValue idVal = new SimpleValue(table);
        idVal.setTypeName(TypeFactory.basic("long").getName());
        id.setValue(idVal);

        Column kpiTimeStamp = new Column("KPI_TIMESTAMP");
        kpiTimeStamp.setSqlType(TypeFactory.basic(java.util.Date.class.getName()).getName());

        Column kpiYear = new Column("KPI_YEAR");
        SimpleValue kpiYearVal = new SimpleValue(table);
        kpiYearVal.setTypeName(TypeFactory.basic("short").getName());
        kpiYear.setLength(4);
        kpiYear.setValue(kpiYearVal);
        addIndex(kpiYear);

        Column kpiQuater = new Column("KPI_QUATER");
        SimpleValue kpiQuaterVal = new SimpleValue(table);
        kpiQuaterVal.setTypeName(TypeFactory.basic("short").getName());
        kpiQuater.setLength(4);
        kpiQuater.setValue(kpiQuaterVal);
        addIndex(kpiQuater);

        Column kpiMonth = new Column("KPI_MONTH");
        SimpleValue kpiMonthVal = new SimpleValue(table);
        kpiMonthVal.setTypeName(TypeFactory.basic("short").getName());
        kpiMonth.setLength(4);
        kpiMonth.setValue(kpiMonthVal);
        addIndex(kpiMonth);

        Column kpiWeek = new Column("KPI_WEEK");
        SimpleValue kpiWeekVal = new SimpleValue(table);
        kpiWeekVal.setTypeName(TypeFactory.basic("short").getName());
        kpiWeek.setLength(4);
        kpiWeek.setValue(kpiWeekVal);
        addIndex(kpiWeek);

        Column kpiDay = new Column("KPI_DAY");
        SimpleValue kpiDayVal = new SimpleValue(table);
        kpiDayVal.setTypeName(TypeFactory.basic("short").getName());
        kpiDay.setLength(4);
        kpiDay.setValue(kpiDayVal);
        addIndex(kpiDay);

        Column kpiHour = new Column("KPI_HOUR");
        SimpleValue kpiHourVal = new SimpleValue(table);
        kpiHourVal.setTypeName(TypeFactory.basic("short").getName());
        kpiHour.setLength(4);
        kpiHour.setValue(kpiHourVal);
        addIndex(kpiHour);

        Column kpiMinute = new Column("KPI_MINUTE");
        SimpleValue kpiMinuteVal = new SimpleValue(table);
        kpiMinuteVal.setTypeName(TypeFactory.basic("short").getName());
        kpiMinute.setLength(4);
        kpiMinute.setValue(kpiMinuteVal);
        addIndex(kpiMinute);

        Column kpiDayOfWeek = new Column("KPI_DAY_OF_WEEK");
        SimpleValue kpiDayOfWeekVal = new SimpleValue(table);
        kpiDayOfWeekVal.setTypeName(TypeFactory.basic("short").getName());
        kpiDayOfWeek.setLength(4);
        kpiDayOfWeek.setValue(kpiDayOfWeekVal);
        addIndex(kpiDayOfWeek);

        Column serverId = new Column("SERVER_ID");
        SimpleValue serverIdVal = new SimpleValue(table);
        serverIdVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        serverId.setValue(serverIdVal);
        serverId.setLength(200);
        addIndex(serverId);

        Column eventId = new Column("EVENT_ID");
        SimpleValue eventIdVal = new SimpleValue(table);
        eventIdVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        eventId.setValue(eventIdVal);
        eventId.setLength(200);
        addIndex(eventId);

        Column eventName = new Column("EVENT_NAME");
        SimpleValue eventNameVal = new SimpleValue(table);
        eventNameVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        eventName.setValue(eventNameVal);
        eventName.setLength(200);
        addIndex(eventName);

        Column processDefinitionId = new Column("PROCESS_DEF_ID");
        SimpleValue processDefinitionIdVal = new SimpleValue(table);
        processDefinitionIdVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        processDefinitionId.setValue(processDefinitionIdVal);
        processDefinitionId.setLength(200);
        addIndex(processDefinitionId);

        Column processDefinitionName = new Column("PROCESS_DEF_NAME");
        SimpleValue processDefinitionNameVal = new SimpleValue(table);
        processDefinitionNameVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        processDefinitionName.setValue(processDefinitionNameVal);
        processDefinitionName.setLength(200);
        addIndex(processDefinitionName);

        Column processDefinitionVersion = new Column("PROCESS_DEF_VERSION");
        SimpleValue processDefinitionVersionVal = new SimpleValue(table);
        processDefinitionVersionVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        processDefinitionVersion.setValue(processDefinitionVersionVal);
        processDefinitionVersion.setLength(200);
        addIndex(processDefinitionVersion);

        Column processInstanceId = new Column("PROCESS_INST_ID");
        SimpleValue processInstanceIdVal = new SimpleValue(table);
        processInstanceIdVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        processInstanceId.setValue(processInstanceIdVal);
        processInstanceId.setLength(200);
        addIndex(processInstanceId);

        Column activityInstanceId = new Column("ACT_INST_ID");
        SimpleValue activityInstanceIdVal = new SimpleValue(table);
        activityInstanceIdVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        activityInstanceId.setValue(activityInstanceIdVal);
        activityInstanceId.setLength(200);
        addIndex(activityInstanceId);

        Column activityInstanceName = new Column("ACT_INST_NAME");
        SimpleValue activityInstanceNameVal = new SimpleValue(table);
        activityInstanceNameVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        activityInstanceName.setValue(activityInstanceNameVal);
        activityInstanceName.setLength(200);
        addIndex(activityInstanceName);

        Column activityInstanceIter = new Column("ACT_INST_ITER");
        SimpleValue activityInstanceIterVal = new SimpleValue(table);
        activityInstanceIterVal.setTypeName(TypeFactory.basic("java.lang.String").getName());
        activityInstanceIter.setValue(activityInstanceIterVal);
        activityInstanceIter.setLength(200);
        addIndex(activityInstanceIter);

        PrimaryKey key = new PrimaryKey();
        key.setName(metaKpi.getCode() + "_PK");
        key.addColumn(id);
        table.setPrimaryKey(key);

        table.addColumn(id);
        table.addColumn(kpiTimeStamp);
        table.addColumn(kpiYear);
        table.addColumn(kpiQuater);
        table.addColumn(kpiMonth);
        table.addColumn(kpiWeek);
        table.addColumn(kpiDay);
        table.addColumn(kpiDayOfWeek);
        table.addColumn(kpiHour);
        table.addColumn(kpiMinute);
        table.addColumn(serverId);
        table.addColumn(eventId);
        table.addColumn(eventName);
        table.addColumn(processDefinitionId);
        table.addColumn(processDefinitionVersion);
        table.addColumn(processDefinitionName);
        table.addColumn(processInstanceId);
        table.addColumn(activityInstanceId);
        table.addColumn(activityInstanceName);
        table.addColumn(activityInstanceIter);

        for (Iterator<MetaDim> i = metaKpi.getMetaDims().iterator(); i.hasNext();) {
            MetaDim metaDim = i.next();
            Column dim = new Column(metaDim.getCode());
            SimpleValue dimVal = new SimpleValue(table);
            dimVal.setTypeName(TypeFactory.basic(metaDim.getValueType()).getName());
            dim.setValue(dimVal);
            if (metaDim.getValueLength() != null) {
                dim.setLength(metaDim.getValueLength());
            }
            table.addColumn(dim);
            addIndex(dim);
        }

        for (Iterator<MetaFact> i = metaKpi.getMetaFacts().iterator(); i.hasNext();) {
            MetaFact metaFact = i.next();
            Column dim = new Column(metaFact.getCode());
            SimpleValue dimVal = new SimpleValue(table);
            dimVal.setTypeName(TypeFactory.basic("java.math.BigDecimal").getName());
            dim.setValue(dimVal);
            table.addColumn(dim);
        }

        if (type == CREATE_SCRIPT) {
            result.add(0, table.sqlCreateString(d, config.buildMapping(), null, null));
        } else if (type == DROP_SCRIPT) {
            result.clear();
            result.add(table.sqlDropString(d, null, null));
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return result;
}

From source file:org.teiid.spring.autoconfigure.RedirectionSchemaInitializer.java

License:Apache License

List<Resource> generatedScripts() {
    List<Resource> resources = Collections.emptyList();

    for (PersistentClass clazz : metadata.getEntityBindings()) {
        org.hibernate.mapping.Table ormTable = clazz.getTable();
        String tableName = ormTable.getQuotedName();
        if (this.schema.getTable(tableName) != null) {
            org.hibernate.mapping.Column c = new org.hibernate.mapping.Column(
                    RedirectionSchemaBuilder.ROW_STATUS_COLUMN);
            c.setSqlTypeCode(TypeFacility.getSQLTypeFromRuntimeType(Integer.class));
            c.setSqlType(JDBCSQLTypeInfo.getTypeName(TypeFacility.getSQLTypeFromRuntimeType(Integer.class)));
            ormTable.addColumn(c);/*from  w  w  w .  j ava  2 s.c  om*/
            ormTable.setName(tableName + TeiidConstants.REDIRECTED_TABLE_POSTFIX);
        }
    }

    List<String> statements = createScript(metadata, dialect, true);
    StringBuilder sb = new StringBuilder();
    for (String s : statements) {
        // we have no need for sequences in the redirected scenario, they are fed from
        // other side.
        if (s.startsWith("drop sequence") || s.startsWith("create sequence")) {
            continue;
        }
        sb.append(s).append(";\n");
    }
    logger.debug("Redirected Schema:\n" + sb.toString());
    resources = Arrays.asList(new ByteArrayResource(sb.toString().getBytes()));
    return resources;
}