Example usage for org.hibernate.tool.hbm2ddl TableMetadata getForeignKeyMetadata

List of usage examples for org.hibernate.tool.hbm2ddl TableMetadata getForeignKeyMetadata

Introduction

In this page you can find the example usage for org.hibernate.tool.hbm2ddl TableMetadata getForeignKeyMetadata.

Prototype

public ForeignKeyMetadata getForeignKeyMetadata(ForeignKey fk) 

Source Link

Usage

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();/*from   ww w .j  a v a2s  .c  o  m*/
        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);

                // 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:org.n52.sos.ds.datasource.CustomConfiguration.java

License:Open Source License

protected List<String> generateConstraintDropScript(final Dialect d, final String c, final String s,
        final DatabaseMetadata m) throws HibernateException {
    final List<String> script = new LinkedList<String>();
    final Iterator<Table> itr = getTableMappings();
    while (itr.hasNext()) {
        final Table table = itr.next();
        // TODO remove because fails if table definition is quoted
        //            final String tableName = table.getQualifiedName(d, c, s);
        if (checkTable(table, m)) {
            @SuppressWarnings("unchecked")
            final Iterator<ForeignKey> subItr = table.getForeignKeyIterator();
            final TableMetadata tableMeta = m.getTableMetadata(table.getName(), s, c, table.isQuoted());
            while (subItr.hasNext()) {
                final ForeignKey fk = subItr.next();
                if (fk.isPhysicalConstraint() && tableMeta.getForeignKeyMetadata(fk) != null) {
                    script.add(fk.sqlDropString(d, c, s));
                }/* w  w  w .ja v a  2  s  .  c o m*/
            }
        }
    }
    return script;
}