Example usage for org.hibernate.mapping Table getName

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

Introduction

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

Prototype

public String getName() 

Source Link

Usage

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.//ww  w .  j a v  a 2 s. co m
 *
 * @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

private void transferForeignKeys(Connection connection, Table fromTable, Table toTable) throws SQLException {
    DatabaseMetadata meta = new DatabaseMetadata(connection, dialect);

    Iterator i = config.getTableMappings();
    while (i.hasNext()) {
        Table t = (Table) i.next();
        Iterator fki = t.getForeignKeyIterator();
        while (fki.hasNext()) {
            ForeignKey fk = (ForeignKey) fki.next();
            Table referencedTable = fk.getReferencedTable();
            if (referencedTable != null && referencedTable == fromTable) {
                TableMetadata tableInfo = meta.getTableMetadata(t.getName(), defaultSchema, defaultCatalog,
                        false);//from   w  w w.  ja  v  a2s.  com

                // verify that the fk is actually in the database.
                if (tableInfo.getForeignKeyMetadata(fk.getName()) == null) {
                    // foreign key does not exist, so do not drop or recreate it.
                    continue;
                }

                String sql = fk.sqlDropString(dialect, defaultCatalog, defaultSchema);
                LOG.info(sql);
                JDBCUtils.execute(connection, sql);
                fk.setReferencedTable(toTable);

                sql = fk.sqlCreateString(dialect, null, defaultCatalog, defaultSchema);
                LOG.info(sql);
                JDBCUtils.execute(connection, sql);
            }
        }
    }
}

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);/*from w  ww  .  jav a2  s .  c o  m*/

    // 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

/**
 * Executes a set of alter table statements to synchronise the underlying database table with the hibernate schema.
 * NOTE: This will only add columns that do not already exist.
 * /*  w w  w. j av  a2s. c  o  m*/
 * @param table         the table to be altered
 * @param connection    the database connection providing access to the table
 * 
 * @throws SQLException is thrown on error
 */
private void updateTableSchema(Table table, Connection connection) throws SQLException {
    DatabaseMetadata meta = new DatabaseMetadata(connection, dialect);
    TableMetadata tableInfo = meta.getTableMetadata(table.getName(), defaultSchema, defaultCatalog, false);

    Iterator alterSqls = table.sqlAlterStrings(dialect, config.getMapping(), tableInfo, defaultCatalog,
            defaultSchema);
    while (alterSqls.hasNext()) {
        String sql = (String) alterSqls.next();
        LOG.info(sql);
        JDBCUtils.execute(connection, sql);
    }
}

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   ww  w .  j a  va2  s .co m
        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:com.zutubi.pulse.master.hibernate.SchemaRefactor.java

License:Apache License

private void metaDropColumnConstraints(Connection connection, Table table, Column column) throws SQLException {
    JDBCUtils.DbType dbType = JDBCUtils.getDBType(connection);
    if (dbType == JDBCUtils.DbType.HSQL) {
        hsqlDropColumnConstraints(connection, table, column);
    } else {/* w  ww . j a  v a 2 s  . co  m*/
        String[] tableNames = JDBCUtils.getSchemaTableNames(connection);
        String metaTableName = null;
        for (String tableName : tableNames) {
            if (tableName.equalsIgnoreCase(table.getName())) {
                metaTableName = tableName;
            }
        }

        if (metaTableName == null) {
            throw new RuntimeException("Could not find table of name '" + table.getName() + "' in schema");
        }

        DatabaseMetaData metaData = connection.getMetaData();
        ResultSet rs = metaData.getIndexInfo(null, null, metaTableName, false, false);
        try {
            while (rs.next()) {
                if (column.getName().equalsIgnoreCase(rs.getString("COLUMN_NAME"))) {
                    String indexName = rs.getString("INDEX_NAME");
                    if (indexName.startsWith("FK") && dbType == JDBCUtils.DbType.MYSQL) {
                        tryDropForeignKey(connection, metaTableName, indexName);
                    }

                    tryDropIndex(connection, metaTableName, indexName);
                }
            }
        } finally {
            JDBCUtils.close(rs);
        }
    }
}

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

License:Apache License

private void hsqlDropColumnConstraints(Connection connection, Table table, Column column) throws SQLException {
    PreparedStatement stmt = null;
    ResultSet rs = null;//from   www . j av  a2  s. co  m
    try {
        stmt = connection.prepareStatement(
                "select FK_NAME from INFORMATION_SCHEMA.SYSTEM_CROSSREFERENCE where FKTABLE_NAME = ? and FKCOLUMN_NAME = ?");
        stmt.setString(1, table.getName());
        stmt.setString(2, column.getName());
        rs = stmt.executeQuery();
        while (rs.next()) {
            String constraintName = rs.getString(1);
            tryDropConstraint(connection, table.getName(), constraintName);
        }
    } finally {
        JDBCUtils.close(rs);
        JDBCUtils.close(stmt);
    }
}

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

License:Apache License

private void sqlDropColumn(Connection connection, Table table, String columnName) throws SQLException {
    String sql;/*ww  w .ja v a  2 s . com*/
    sql = "alter table " + table.getName() + " drop column " + columnName;
    LOG.info(sql);
    JDBCUtils.execute(connection, sql);
}

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

License:Apache License

protected Table copyTable(Connection connection, Table fromTable, String toTableName) throws SQLException {
    Table toTable = clone(fromTable);// w  ww .  ja va 2 s  .  c  o  m
    toTable.setName(toTableName);

    String sql = toTable.sqlCreateString(dialect, config.getMapping(), defaultCatalog, defaultSchema);
    LOG.info(sql);
    JDBCUtils.execute(connection, sql);

    // if there is data to transfer..
    if (JDBCUtils.executeCount(connection, "select * from " + fromTable.getName()) > 0) {
        String columnSql = "";
        String sep = "";
        Iterator columns = toTable.getColumnIterator();
        while (columns.hasNext()) {
            Column column = (Column) columns.next();
            columnSql = columnSql + sep + column.getName();
            sep = ",";
        }

        sql = "insert into " + toTableName + "(" + columnSql + ") select " + columnSql + " from "
                + fromTable.getName();
        LOG.info(sql);
        JDBCUtils.execute(connection, sql);
    }

    config.addTable(toTable);
    return toTable;
}

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());/*from w w w.j  a v  a 2  s.c  o 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;
}