Example usage for org.hibernate.mapping Column getValue

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

Introduction

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

Prototype

public Value getValue() 

Source Link

Usage

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

License:Open Source License

protected RootClass createTableMapping(Mappings mappings,
        com.manydesigns.portofino.model.database.Table aTable) {

    Table tab = mappings.addTable(escapeName(aTable.getSchemaName()), null, escapeName(aTable.getTableName()),
            null, false);/*from w  w  w.  ja v a2 s. c  o m*/
    //tab.setName(escapeName(aTable.getTableName()));
    //tab.setSchema(escapeName(aTable.getSchemaName()));
    mappings.addTableBinding(aTable.getSchemaName(), null, aTable.getTableName(), aTable.getTableName(), null);

    RootClass clazz = new RootClass();
    clazz.setEntityName(aTable.getActualEntityName());
    clazz.setJpaEntityName(aTable.getActualEntityName());
    if (aTable.getJavaClass() != null) {
        clazz.setClassName(aTable.getJavaClass());
        clazz.setProxyInterfaceName(aTable.getJavaClass());
    }
    clazz.setLazy(LAZY);
    clazz.setTable(tab);
    //clazz.setNodeName(aTable.getTableName());

    List<com.manydesigns.portofino.model.database.Column> columnList = new ArrayList<com.manydesigns.portofino.model.database.Column>();

    for (com.manydesigns.portofino.model.database.Column modelColumn : aTable.getColumns()) {
        int jdbcType = modelColumn.getJdbcType();
        Class javaType = modelColumn.getActualJavaType();

        //First param = null ==> doesn't really set anything, just check
        boolean hibernateTypeOk = setHibernateType(null, modelColumn, javaType, jdbcType);
        if (hibernateTypeOk) {
            columnList.add(modelColumn);
        } else {
            logger.error(
                    "Cannot find Hibernate type for table: {}, column: {}, jdbc type: {}, type name: {}. Skipping column.",
                    new Object[] { aTable.getQualifiedName(), modelColumn.getColumnName(), jdbcType,
                            javaType != null ? javaType.getName() : null });
        }
    }

    //Primary keys
    List<com.manydesigns.portofino.model.database.Column> columnPKList = aTable.getPrimaryKey().getColumns();

    if (!columnList.containsAll(columnPKList)) {
        logger.error("Primary key refers to some invalid columns, skipping table {}",
                aTable.getQualifiedName());
        return null;
    }

    if (columnPKList.size() > 1) {
        createPKComposite(mappings, aTable, aTable.getPrimaryKey().getPrimaryKeyName(), clazz, tab,
                columnPKList);
    } else {
        createPKSingle(mappings, aTable, aTable.getPrimaryKey().getPrimaryKeyName(), clazz, tab, columnPKList);
    }

    //Other columns
    columnList.removeAll(columnPKList);

    for (com.manydesigns.portofino.model.database.Column column : columnList) {
        Column col = createColumn(mappings, tab, column);
        if (col != null) {
            clazz.addProperty(createProperty(column, col.getValue()));
        }
    }

    return clazz;
}

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

License:Open Source License

protected void createPKComposite(Mappings mappings, com.manydesigns.portofino.model.database.Table mdTable,
        String pkName, RootClass clazz, Table tab,
        List<com.manydesigns.portofino.model.database.Column> columnPKList) {

    PrimaryKey primaryKey = new PrimaryKey();
    primaryKey.setName(pkName);//from   w  ww . j a  v a 2 s . c o m
    primaryKey.setTable(tab);

    clazz.setEmbeddedIdentifier(true);
    Component component = new Component(mappings, clazz);
    component.setDynamic(mdTable.getActualJavaClass() == null);
    String name;
    name = mdTable.getQualifiedName();

    component.setRoleName(name + ".id");
    component.setEmbedded(true);
    //component.setNodeName("id");
    component.setKey(true);
    component.setNullValue("undefined");

    if (!component.isDynamic()) {
        component.setComponentClassName(mdTable.getJavaClass()); //TODO verificare se non si intende actualJavaClass
    }

    boolean hasErrors = false;
    for (com.manydesigns.portofino.model.database.Column column : columnPKList) {
        if (column == null) {
            throw new InternalError("Null column");
        }

        Column col = createColumn(mappings, tab, column);

        hasErrors = col == null || hasErrors;

        if (col != null) {
            primaryKey.addColumn(col);
            Property prop = createProperty(column, col.getValue());
            prop.setCascade("none");
            //prop.setPropertyAccessorName("property"); interferisce con il generator pi sotto
            prop.setPersistentClass(clazz);
            component.addProperty(prop);

            //Generator not supported for embedded map identifier
            //See https://forum.hibernate.org/viewtopic.php?t=945273
            //See Component.buildIdentifierGenerator()
            /*String columnName = column.getColumnName();
            PrimaryKeyColumn pkCol = mdTable.getPrimaryKey().findPrimaryKeyColumnByName(columnName);
            if(pkCol == null) {
            logger.error("Column without corresponding PrimaryKeyColumn: {}", columnName);
            hasErrors = true;
            continue;
            }
            Generator generator = pkCol.getGenerator();
            setPKColumnGenerator(mappings, clazz, tab, column, value, generator);*/
        }
    }
    if (hasErrors) {
        // TODO PAOLO: se la PK non e' buona, tutta la tabella dovrebbe saltare
        logger.error("Skipping primary key");
        return;
    }

    tab.setIdentifierValue(component);
    clazz.setIdentifier(component);
    clazz.setDiscriminatorValue(name);

    tab.setPrimaryKey(primaryKey);
}

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

License:Open Source License

protected void createPKSingle(Mappings mappings, com.manydesigns.portofino.model.database.Table mdTable,
        String pkName, RootClass clazz, Table tab,
        List<com.manydesigns.portofino.model.database.Column> columnPKList) {
    PrimaryKeyColumn pkcol = mdTable.getPrimaryKey().getPrimaryKeyColumns().get(0);
    com.manydesigns.portofino.model.database.Column column = columnPKList.get(0);
    final PrimaryKey primaryKey = new PrimaryKey();
    primaryKey.setName(pkName);//from  www.j av  a  2 s  .  co m
    primaryKey.setTable(tab);
    tab.setPrimaryKey(primaryKey);

    Column col = createColumn(mappings, tab, column);

    if (col == null) {
        // TODO PAOLO: se la PK non e' buona, tutta la tabella dovrebbe saltare
        logger.error("Skipping primary key");
        return;
    }

    SimpleValue id = (SimpleValue) col.getValue();
    //Make the defaults explicit. See section 5.1.4.5. Assigned identifiers in the Hibernate reference
    //(http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html)
    id.setIdentifierGeneratorStrategy("assigned");
    id.setNullValue("undefined");

    tab.getPrimaryKey().addColumn(col);

    Property prop = createProperty(column, id);
    clazz.addProperty(prop);
    prop.setPropertyAccessorName(mappings.getDefaultAccess());
    //PropertyGeneration generation = PropertyGeneration.parse(null);
    //prop.setGeneration(generation);

    prop.setInsertable(false);
    prop.setUpdateable(false);

    Generator generator = pkcol.getGenerator();

    setPKColumnGenerator(mappings, clazz, tab, column, id, generator);

    tab.setIdentifierValue(id);
    clazz.setIdentifier(id);
    clazz.setIdentifierProperty(prop);
    clazz.setDiscriminatorValue(mdTable.getQualifiedName());

}

From source file:com.vecna.maven.hibernate.HibernateDocMojo.java

License:Apache License

/**
 * set a comment on Hibernate columns/*  ww  w  . j av  a2  s .c o  m*/
 */
private void setComment(String comment, Iterator<Column> columnIterator) {
    while (columnIterator.hasNext()) {
        Column column = columnIterator.next();
        if (encryptedTypeRegex != null && column.getValue() instanceof SimpleValue) {
            String typeName = ((SimpleValue) column.getValue()).getTypeName();
            if (typeName != null && typeName.matches(encryptedTypeRegex)) {
                comment += " [encrypted]";
            }
        }
        column.setComment(comment);
    }
}

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

License:Apache License

/**
 * Tests that single- and multi-column user type mappings work
 * correctly. Also Checks that the "sqlType" property is honoured.
 *//* ww w. j  av a2  s .  co  m*/
public void testUserTypeMappings() {
    DefaultGrailsDomainConfiguration config = getDomainConfig(MULTI_COLUMN_USER_TYPE_DEFINITION);
    PersistentClass persistentClass = config.getClassMapping("Item");

    // First check the "name" property and its associated column.
    Property nameProperty = persistentClass.getProperty("name");
    assertEquals(1, nameProperty.getColumnSpan());
    assertEquals("name", nameProperty.getName());

    Column column = (Column) nameProperty.getColumnIterator().next();
    assertEquals("s_name", column.getName());
    assertEquals("text", column.getSqlType());

    // Next the "other" property.
    Property otherProperty = persistentClass.getProperty("other");
    assertEquals(1, otherProperty.getColumnSpan());
    assertEquals("other", otherProperty.getName());

    column = (Column) otherProperty.getColumnIterator().next();
    assertEquals("other", column.getName());
    assertEquals("wrapper-characters", column.getSqlType());
    assertEquals(MyUserType.class.getName(), column.getValue().getType().getName());
    assertTrue(column.getValue() instanceof SimpleValue);
    SimpleValue v = (SimpleValue) column.getValue();
    assertEquals("myParam1", v.getTypeParameters().get("param1"));
    assertEquals("myParam2", v.getTypeParameters().get("param2"));

    // And now for the "price" property, which should have two
    // columns.
    Property priceProperty = persistentClass.getProperty("price");
    assertEquals(2, priceProperty.getColumnSpan());
    assertEquals("price", priceProperty.getName());

    Iterator colIter = priceProperty.getColumnIterator();
    column = (Column) colIter.next();
    assertEquals("value", column.getName());
    assertNull("SQL type should have been 'null' for 'value' column.", column.getSqlType());

    column = (Column) colIter.next();
    assertEquals("currency_code", column.getName());
    assertEquals("text", column.getSqlType());
}

From source file:org.jboss.tools.hibernate.ui.view.HibernateUtils.java

License:Open Source License

public static Table getTable(Column column) {
    if (column.getValue() != null) {
        return column.getValue().getTable();
    }/* w w w  .ja v a2  s .com*/
    return null;
}

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);
            }/*  w  ww  .j a  v  a  2s . c om*/
        }
    } 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");
          }
        }
                
      }
      } */
}