Search in sources :

Example 1 with ForeignKeyInformation

use of org.hibernate.tool.schema.extract.spi.ForeignKeyInformation in project hibernate-orm by hibernate.

the class InformationExtractorJdbcDatabaseMetaDataImpl method getForeignKeys.

@Override
public Iterable<ForeignKeyInformation> getForeignKeys(TableInformation tableInformation) {
    final Map<Identifier, ForeignKeyBuilder> fkBuilders = new HashMap<>();
    final QualifiedTableName tableName = tableInformation.getName();
    final Identifier catalog = tableName.getCatalogName();
    final Identifier schema = tableName.getSchemaName();
    final String catalogFilter;
    final String schemaFilter;
    if (catalog == null) {
        catalogFilter = "";
    } else {
        catalogFilter = catalog.getText();
    }
    if (schema == null) {
        schemaFilter = "";
    } else {
        schemaFilter = schema.getText();
    }
    try {
        ResultSet resultSet = extractionContext.getJdbcDatabaseMetaData().getImportedKeys(catalogFilter, schemaFilter, tableInformation.getName().getTableName().getText());
        try {
            while (resultSet.next()) {
                // IMPL NOTE : The builder is mainly used to collect the column reference mappings
                final Identifier fkIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("FK_NAME"));
                ForeignKeyBuilder fkBuilder = fkBuilders.get(fkIdentifier);
                if (fkBuilder == null) {
                    fkBuilder = generateForeignKeyBuilder(fkIdentifier);
                    fkBuilders.put(fkIdentifier, fkBuilder);
                }
                final QualifiedTableName incomingPkTableName = extractKeyTableName(resultSet, "PK");
                final TableInformation pkTableInformation = extractionContext.getDatabaseObjectAccess().locateTableInformation(incomingPkTableName);
                if (pkTableInformation == null) {
                    // should match.
                    continue;
                }
                final Identifier fkColumnIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("FKCOLUMN_NAME"));
                final Identifier pkColumnIdentifier = DatabaseIdentifier.toIdentifier(resultSet.getString("PKCOLUMN_NAME"));
                fkBuilder.addColumnMapping(tableInformation.getColumn(fkColumnIdentifier), pkTableInformation.getColumn(pkColumnIdentifier));
            }
        } finally {
            resultSet.close();
        }
    } catch (SQLException e) {
        throw convertSQLException(e, "Error accessing column metadata: " + tableInformation.getName().toString());
    }
    final List<ForeignKeyInformation> fks = new ArrayList<ForeignKeyInformation>();
    for (ForeignKeyBuilder fkBuilder : fkBuilders.values()) {
        ForeignKeyInformation fk = fkBuilder.build();
        fks.add(fk);
    }
    return fks;
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) ForeignKeyInformation(org.hibernate.tool.schema.extract.spi.ForeignKeyInformation) DatabaseIdentifier(org.hibernate.boot.model.naming.DatabaseIdentifier) Identifier(org.hibernate.boot.model.naming.Identifier) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation)

Example 2 with ForeignKeyInformation

use of org.hibernate.tool.schema.extract.spi.ForeignKeyInformation in project hibernate-orm by hibernate.

the class AbstractSchemaMigrator method applyForeignKeys.

protected void applyForeignKeys(Table table, TableInformation tableInformation, Dialect dialect, Metadata metadata, Formatter formatter, ExecutionOptions options, GenerationTarget... targets) {
    if (dialect.hasAlterTable()) {
        final Exporter<ForeignKey> exporter = dialect.getForeignKeyExporter();
        @SuppressWarnings("unchecked") final Iterator<ForeignKey> fkItr = table.getForeignKeyIterator();
        while (fkItr.hasNext()) {
            final ForeignKey foreignKey = fkItr.next();
            if (foreignKey.isPhysicalConstraint() && foreignKey.isCreationEnabled()) {
                ForeignKeyInformation existingForeignKey = null;
                if (tableInformation != null) {
                    existingForeignKey = findMatchingForeignKey(foreignKey, tableInformation);
                }
                if (existingForeignKey == null) {
                    // todo : shouldn't we just drop+recreate if FK exists?
                    //		this follows the existing code from legacy SchemaUpdate which just skipped
                    // in old SchemaUpdate code, this was the trigger to "create"
                    applySqlStrings(false, exporter.getSqlCreateStrings(foreignKey, metadata), formatter, options, targets);
                }
            }
        }
    }
}
Also used : ForeignKeyInformation(org.hibernate.tool.schema.extract.spi.ForeignKeyInformation) ForeignKey(org.hibernate.mapping.ForeignKey)

Example 3 with ForeignKeyInformation

use of org.hibernate.tool.schema.extract.spi.ForeignKeyInformation in project hibernate-orm by hibernate.

the class TableInformationImpl method foreignKeys.

protected Map<Identifier, ForeignKeyInformation> foreignKeys() {
    if (foreignKeys == null) {
        final Map<Identifier, ForeignKeyInformation> fkMap = new HashMap<>();
        final Iterable<ForeignKeyInformation> fks = extractor.getForeignKeys(this);
        for (ForeignKeyInformation fk : fks) {
            fkMap.put(fk.getForeignKeyIdentifier(), fk);
        }
        this.foreignKeys = fkMap;
    }
    return foreignKeys;
}
Also used : ForeignKeyInformation(org.hibernate.tool.schema.extract.spi.ForeignKeyInformation) Identifier(org.hibernate.boot.model.naming.Identifier) HashMap(java.util.HashMap)

Aggregations

ForeignKeyInformation (org.hibernate.tool.schema.extract.spi.ForeignKeyInformation)3 HashMap (java.util.HashMap)2 Identifier (org.hibernate.boot.model.naming.Identifier)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 DatabaseIdentifier (org.hibernate.boot.model.naming.DatabaseIdentifier)1 QualifiedTableName (org.hibernate.boot.model.relational.QualifiedTableName)1 ForeignKey (org.hibernate.mapping.ForeignKey)1 TableInformation (org.hibernate.tool.schema.extract.spi.TableInformation)1