Example usage for org.hibernate.mapping ForeignKey setTable

List of usage examples for org.hibernate.mapping ForeignKey setTable

Introduction

In this page you can find the example usage for org.hibernate.mapping ForeignKey setTable.

Prototype

public void setTable(Table table) 

Source Link

Usage

From source file:com.opengamma.util.db.management.AbstractDbManagement.java

License:Open Source License

@Override
public void dropSchema(String catalog, String schema) {
    // Does not handle triggers or stored procedures yet
    ArrayList<String> script = new ArrayList<String>();

    Connection conn = null;/* ww w. j a va 2s.  c o  m*/
    try {
        if (!getCatalogCreationStrategy().catalogExists(catalog)) {
            System.out.println("Catalog " + catalog + " does not exist");
            return; // nothing to drop
        }

        conn = connect(catalog);

        if (schema != null) {
            Statement statement = conn.createStatement();
            Collection<String> schemas = getAllSchemas(catalog, statement);
            statement.close();

            if (!schemas.contains(schema)) {
                System.out.println("Schema " + schema + " does not exist");
                return; // nothing to drop
            }
        }

        setActiveSchema(conn, schema);
        Statement statement = conn.createStatement();

        // Drop constraints SQL
        if (getHibernateDialect().dropConstraints()) {
            for (Pair<String, String> constraint : getAllForeignKeyConstraints(catalog, schema, statement)) {
                String name = constraint.getFirst();
                String table = constraint.getSecond();
                ForeignKey fk = new ForeignKey();
                fk.setName(name);
                fk.setTable(new Table(table));

                String dropConstraintSql = fk.sqlDropString(getHibernateDialect(), null, schema);
                script.add(dropConstraintSql);
            }
        }

        // Drop views SQL
        for (String name : getAllViews(catalog, schema, statement)) {
            Table table = new Table(name);
            String dropViewStr = table.sqlDropString(getHibernateDialect(), null, schema);
            dropViewStr = dropViewStr.replaceAll("drop table", "drop view");
            script.add(dropViewStr);
        }

        // Drop tables SQL
        for (String name : getAllTables(catalog, schema, statement)) {
            Table table = new Table(name);
            String dropTableStr = table.sqlDropString(getHibernateDialect(), null, schema);
            script.add(dropTableStr);
        }

        // Now execute it all
        statement.close();
        statement = conn.createStatement();
        for (String sql : script) {
            //System.out.println("Executing \"" + sql + "\"");
            statement.executeUpdate(sql);
        }

        statement.close();
        statement = conn.createStatement();

        // Drop sequences SQL
        script.clear();
        for (String name : getAllSequences(catalog, schema, statement)) {
            final SequenceStructure sequenceStructure = new SequenceStructure(getHibernateDialect(), name, 0, 1,
                    Long.class);
            String[] dropSequenceStrings = sequenceStructure.sqlDropStrings(getHibernateDialect());
            script.addAll(Arrays.asList(dropSequenceStrings));
        }

        //now execute drop sequence
        statement.close();
        statement = conn.createStatement();
        for (String sql : script) {
            //System.out.println("Executing \"" + sql + "\"");
            statement.executeUpdate(sql);
        }

        statement.close();

    } catch (SQLException e) {
        throw new OpenGammaRuntimeException("Failed to drop schema", e);
    } finally {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
        }
    }
}

From source file:com.opengamma.util.db.management.SqlServer2008DbManagement.java

License:Open Source License

@Override
public void dropSchema(String catalog, String schema) {
    // Does not handle triggers or stored procedures yet
    ArrayList<String> script = new ArrayList<String>();

    Connection conn = null;/*from  ww w . ja  v a  2  s  .c o  m*/
    try {
        if (!getCatalogCreationStrategy().catalogExists(catalog)) {
            System.out.println("Catalog " + catalog + " does not exist");
            return; // nothing to drop
        }

        conn = connect(catalog);

        if (schema != null) {
            Statement statement = conn.createStatement();
            Collection<String> schemas = getAllSchemas(catalog, statement);
            statement.close();

            if (!schemas.contains(schema)) {
                System.out.println("Schema " + schema + " does not exist");
                return; // nothing to drop
            }
        }

        setActiveSchema(conn, schema);
        Statement statement = conn.createStatement();

        // Drop constraints SQL
        if (getHibernateDialect().dropConstraints()) {
            for (Pair<String, String> constraint : getAllForeignKeyConstraints(catalog, schema, statement)) {
                String name = constraint.getFirst();
                String table = constraint.getSecond();
                ForeignKey fk = new ForeignKey();
                fk.setName(name);
                fk.setTable(new Table(table));

                String dropConstraintSql = fk.sqlDropString(getHibernateDialect(), null, schema);
                script.add(dropConstraintSql);
            }
        }

        // Drop views SQL
        for (String name : getAllViews(catalog, schema, statement)) {
            Table table = new Table(name);
            String dropViewStr = table.sqlDropString(getHibernateDialect(), null, schema);
            dropViewStr = dropViewStr.replaceAll("drop table", "drop view");
            script.add(dropViewStr);
        }

        // Drop tables SQL
        for (String name : getAllTables(catalog, schema, statement)) {
            Table table = new Table(name);
            String dropTableStr = table.sqlDropString(getHibernateDialect(), null, schema);
            script.add(dropTableStr);
        }

        // Now execute it all
        statement.close();
        statement = conn.createStatement();
        for (String sql : script) {
            //System.out.println("Executing \"" + sql + "\"");
            statement.executeUpdate(sql);
        }

        statement.close();
        statement = conn.createStatement();

        // Drop sequences SQL
        script.clear();
        for (String name : getAllSequences(catalog, schema, statement)) {
            Table table = new Table(name);
            String dropTableStr = table.sqlDropString(getHibernateDialect(), null, schema);
            script.add(dropTableStr);
        }

        //now execute drop sequence
        statement.close();
        statement = conn.createStatement();
        for (String sql : script) {
            //System.out.println("Executing \"" + sql + "\"");
            statement.executeUpdate(sql);
        }

        statement.close();

    } catch (SQLException e) {
        throw new OpenGammaRuntimeException("Failed to drop schema", e);
    } finally {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
        }
    }
}

From source file:net.lshift.hibernate.migrations.AlterTableBuilder.java

License:Apache License

public AlterTableBuilder addForeignKey(String name, String[] columnNames, String referencedTable,
        String[] referencedColumns) {
    ForeignKey fk = new ForeignKey();
    fk.setName(name);/*from   w w  w .j  a  va2 s. c  o  m*/
    for (String col : columnNames)
        fk.addColumn(new Column(col));
    fk.setTable(new Table(table));

    PrimaryKey refPrimaryKey = new PrimaryKey();
    for (String col : referencedColumns)
        refPrimaryKey.addColumn(new Column(col));
    Table refTable = new Table(referencedTable);
    refTable.setPrimaryKey(refPrimaryKey);

    fk.setReferencedTable(refTable);

    String defaultCatalog = config.getProperties().getProperty(Environment.DEFAULT_CATALOG);
    String defaultSchema = config.getProperties().getProperty(Environment.DEFAULT_SCHEMA);

    // fk.sqlConstraintString appears to generate incorrect SQL against MySQL in some instances.
    // The referenced columns are not always correctly listed.
    //    alterFragments.add(" add index " + fk.getName() + " (" + StringHelper.join(", ", columnNames) +
    //        "), add constraint " + fk.getName() + " foreign key (" + StringHelper.join(", ", columnNames) +
    //        " references " + referencedTable + " (" + StringHelper.join(", ", referencedColumns) + ")");
    alterFragments.add(fk.sqlConstraintString(dialect, fk.getName(), defaultCatalog, defaultSchema));
    return this;
}