Example usage for org.hibernate.mapping Column getName

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

Introduction

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

Prototype

public String getName() 

Source Link

Usage

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  w w  w .j a  va  2 s.  c om*/
    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

protected Table copyTable(Connection connection, Table fromTable, String toTableName) throws SQLException {
    Table toTable = clone(fromTable);/*from  w w  w . j  a  v  a2 s  . c om*/
    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.transfer.jdbc.HibernateTransferSource.java

License:Apache License

private void exportTable(TransferTarget target, Table table, Connection con, List<Column> columns)
        throws TransferException, SQLException {
    target.startTable(new HibernateTable(table));

    String sql = MappingUtils.sqlSelectAll(table);

    PreparedStatement statement = null;
    ResultSet rows = null;/* w w  w.j  av  a  2  s.  c  om*/
    try {
        statement = con.prepareStatement(sql);

        rows = statement.executeQuery();
        while (rows.next()) {
            Map<String, Object> row = new HashMap<String, Object>();
            for (Column column : columns) {
                row.put(column.getName(), rows.getObject(column.getName()));
            }
            target.row(row);
        }
    } finally {
        JDBCUtils.close(rows);
        JDBCUtils.close(statement);
    }

    target.endTable();
}

From source file:com.zutubi.pulse.master.transfer.TransferAPI.java

License:Apache License

/**
 * Run some diagnostics on the connected database, checking that all of the tables and columns in our mapping
 * are present and accounted for.//  www. j av a2  s .  co  m
 *
 * @param configuration the hibernate schema mappings.
 * @param dataSource    the datasource for the database being verified.
 */
private boolean isSchemaMappingValid(Configuration configuration, DataSource dataSource)
        throws TransferException {
    Connection con = null;
    try {
        try {
            con = dataSource.getConnection();
        } catch (SQLException e) {
            throw new TransferException(
                    "Failed to connect to the configured dataSource.  Cause: '" + e.getMessage() + "'");
        }

        // we could use the configuration.validateSchema() except that the output is not as friendly or comprehensive
        // as we would like - reporting only first error that it encounters.  The downside to the custom implementation
        // is that it is not as comprehensive in its tests.

        // output diagnostics.
        StringBuilder builder = new StringBuilder();
        builder.append("The following expected schema entities are missing from the source database:\n");

        boolean problemDetected = false;
        boolean noTablesDetected = true;
        Iterator tables = configuration.getTableMappings();
        while (tables.hasNext()) {
            Table table = (Table) tables.next();
            String tableName = table.getName();
            if (!JDBCUtils.tableExists(con, tableName)) {
                // table is missing.
                builder.append("  Table ").append(tableName).append(" is missing\n");
                problemDetected = true;
                continue;
            }

            noTablesDetected = false;

            List<Column> missingColumns = new LinkedList<Column>();
            List<Column> columns = MappingUtils.getColumns(table);
            for (Column column : columns) {
                String columnName = column.getName();
                if (!JDBCUtils.columnExists(con, tableName, columnName)) {
                    missingColumns.add(column);
                    problemDetected = true;
                }
            }
            if (missingColumns.size() > 0) {
                builder.append("  Table ").append(tableName).append(" is missing column(s): ");
                String sep = "";
                for (Column column : missingColumns) {
                    builder.append(sep).append(column.getName());
                    sep = ", ";
                }
                builder.append("\n");
            }
        }
        if (problemDetected) {
            if (noTablesDetected) {
                System.err.println("The pulse schema could not be located. "
                        + "Please check that your database configuration details are correct.");
            } else {
                System.err.println(builder.toString());
            }
        }
        return !problemDetected;
    } finally {
        JDBCUtils.close(con);
    }
}

From source file:corner.migration.services.impl.fragment.ChangeColumnFragment.java

License:Apache License

String sqlAlterStrings(Table table, String columnName, Dialect dialect, Mapping p, TableMetadata tableInfo,
        String defaultCatalog, String defaultSchema) throws HibernateException {

    Iterator iter = table.getColumnIterator();
    String alterSQL = null;// w  w w.ja v  a  2s . co m

    while (iter.hasNext()) {
        Column column = (Column) iter.next();
        ColumnMetadata columnInfo = tableInfo.getColumnMetadata(column.getName());
        if (columnInfo != null && columnInfo.getName().toLowerCase().equals(columnName.toLowerCase())) {//,???
            alterSQL = this.getConnectionAdapter().changeColumnSQL(this.getQualifiedTableName(table),
                    getSQLType(column));
        }

    }
    return alterSQL;
}

From source file:corner.services.migration.impl.fragment.ChangeColumnFragment.java

License:Apache License

String sqlAlterStrings(Table table, String columnName, Dialect dialect, Mapping p, TableMetadata tableInfo,
        String defaultCatalog, String defaultSchema) throws HibernateException {

    Iterator iter = table.getColumnIterator();
    String alterSQL = null;// w  ww . j a  va 2 s. c o m

    while (iter.hasNext()) {
        Column column = (Column) iter.next();
        ColumnMetadata columnInfo = tableInfo.getColumnMetadata(column.getName());
        if (columnInfo != null && columnInfo.getName().toLowerCase().equals(columnName.toLowerCase())) {//,???
            alterSQL = this.getConnectionAdapter().changeColumnSQL(table.getName(), getSQLType(column));
        }

    }
    return alterSQL;
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.elasticmi.HbMappedClass.java

License:Open Source License

/**
 * create a {@link HbMappedProperty} for tables that do not have a corresponding entity class
 * (e.g. HIST*)//from   ww  w.  j a va2s.  c o  m
 * Uses meta information of the {@link Column} instead.
 * 
 */
private void createProperty(Column column) {
    HbMappedProperty newHbmProp = new HbMappedProperty(this, null, column);
    properties.put(column.getName(), newHbmProp);
}

From source file:de.xwic.sandbox.server.installer.modules.UpdateColumnSizes.java

License:Apache License

public void run(InstallationManager manager) throws Exception {

    IDatabaseHandler dbHandler = manager.getDatabaseHandler();

    Configuration cfg = HibernateUtil.getConfiguration();

    for (Iterator<?> it = cfg.getTableMappings(); it.hasNext();) {

        Table table = (Table) it.next();
        List<DbColumn> list = dbHandler.getTableColumns(table.getName());
        // put it into a map for easier access
        Map<String, DbColumn> map = new HashMap<String, DbColumn>();
        for (Iterator<DbColumn> itL = list.iterator(); itL.hasNext();) {
            DbColumn col = itL.next();//from  w  ww  .j a  v a  2  s. com
            map.put(col.getName(), col);
        }

        for (Iterator<?> itC = table.getColumnIterator(); itC.hasNext();) {
            Column column = (Column) itC.next();
            DbColumn dbCol = map.get(column.getName());
            if (dbCol != null && "varchar".equalsIgnoreCase(dbCol.getDataType())
                    && dbCol.getLength() != column.getLength()) {
                if (dbCol.getLength() > column.getLength()) {
                    log.warn("The column '" + column.getName() + "' in table '" + table.getName()
                            + "' is larger (" + dbCol.getLength() + ") then specified (" + column.getLength()
                            + ") in the mapping. The column size is not modified. ");
                } else {
                    log.info("Changing column size for '" + table.getName() + "." + column.getName() + "' from "
                            + dbCol.getLength() + " to " + column.getLength());
                    dbHandler.alterColumn(table.getName(), dbCol.getName(),
                            "VARCHAR(" + column.getLength() + ")");
                }
            }
        }

    }

}

From source file:edu.wustl.common.util.dbManager.HibernateMetaData.java

License:BSD License

public static String getColumnName(Class classObj, String attributeName) {
    //Logger.out.debug("classObj, String attributeName "+classObj+" "+attributeName);
    Iterator it = cfg.getClassMapping(classObj.getName()).getPropertyClosureIterator();
    while (it.hasNext()) {
        Property property = (Property) it.next();

        //Logger.out.debug("property.getName() "+property.getName());
        //System.out.println();
        //System.out.print("property.getName() "+property.getName()+" ");
        if (property != null && property.getName().equals(attributeName)) {
            //System.out.println("property.getColumnSpan() "+property.getColumnSpan());
            Iterator colIt = property.getColumnIterator();
            while (colIt.hasNext()) {
                Column col = (Column) colIt.next();
                //System.out.println("col "+col.getName());
                return col.getName();
            }/*  w w  w  . ja va  2 s . com*/
        }
    }

    Property property = cfg.getClassMapping(classObj.getName()).getIdentifierProperty();
    //Logger.out.debug("property.getName() "+property.getName());
    if (property.getName().equals(attributeName)) {
        Iterator colIt = property.getColumnIterator();//y("id").getColumnIterator();
        while (colIt.hasNext()) {
            Column col = (Column) colIt.next();
            return col.getName();
            //System.out.println(col.getName());
        }
    }

    return "";
}

From source file:edu.wustl.common.util.dbManager.HibernateMetaData.java

License:BSD License

/**
 * This function gets the key Id/*  ww w  . j  av  a 2  s  .  c om*/
 * from hibernate mappings and returns the value
 * @param attributeName
 * @return key Id
 *
 */
public static String getKeyId(String attributeName) {
    org.hibernate.mapping.Collection col1 = cfg.getCollectionMapping(attributeName);
    Iterator keyIt = col1.getKey().getColumnIterator();
    while (keyIt.hasNext()) {
        Column col = (Column) keyIt.next();
        return (col.getName());
    }

    return "";
}