Search in sources :

Example 6 with ForeignKey

use of org.hibernate.mapping.ForeignKey in project hibernate-orm by hibernate.

the class InFlightMetadataCollectorImpl method secondPassCompileForeignKeys.

protected void secondPassCompileForeignKeys(final Table table, Set<ForeignKey> done, final MetadataBuildingContext buildingContext) throws MappingException {
    table.createForeignKeys();
    Iterator itr = table.getForeignKeyIterator();
    while (itr.hasNext()) {
        final ForeignKey fk = (ForeignKey) itr.next();
        if (!done.contains(fk)) {
            done.add(fk);
            final String referencedEntityName = fk.getReferencedEntityName();
            if (referencedEntityName == null) {
                throw new MappingException("An association from the table " + fk.getTable().getName() + " does not specify the referenced entity");
            }
            log.debugf("Resolving reference to class: %s", referencedEntityName);
            final PersistentClass referencedClass = getEntityBinding(referencedEntityName);
            if (referencedClass == null) {
                throw new MappingException("An association from the table " + fk.getTable().getName() + " refers to an unmapped class: " + referencedEntityName);
            }
            if (referencedClass.isJoinedSubclass()) {
                secondPassCompileForeignKeys(referencedClass.getSuperclass().getTable(), done, buildingContext);
            }
            fk.setReferencedTable(referencedClass.getTable());
            Identifier nameIdentifier;
            ImplicitForeignKeyNameSource foreignKeyNameSource = new ImplicitForeignKeyNameSource() {

                final List<Identifier> columnNames = extractColumnNames(fk.getColumns());

                List<Identifier> referencedColumnNames = null;

                @Override
                public Identifier getTableName() {
                    return table.getNameIdentifier();
                }

                @Override
                public List<Identifier> getColumnNames() {
                    return columnNames;
                }

                @Override
                public Identifier getReferencedTableName() {
                    return fk.getReferencedTable().getNameIdentifier();
                }

                @Override
                public List<Identifier> getReferencedColumnNames() {
                    if (referencedColumnNames == null) {
                        referencedColumnNames = extractColumnNames(fk.getReferencedColumns());
                    }
                    return referencedColumnNames;
                }

                @Override
                public Identifier getUserProvidedIdentifier() {
                    return fk.getName() != null ? Identifier.toIdentifier(fk.getName()) : null;
                }

                @Override
                public MetadataBuildingContext getBuildingContext() {
                    return buildingContext;
                }
            };
            nameIdentifier = getMetadataBuildingOptions().getImplicitNamingStrategy().determineForeignKeyName(foreignKeyNameSource);
            fk.setName(nameIdentifier.render(getDatabase().getJdbcEnvironment().getDialect()));
            fk.alignColumns();
        }
    }
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) Iterator(java.util.Iterator) ImplicitForeignKeyNameSource(org.hibernate.boot.model.naming.ImplicitForeignKeyNameSource) List(java.util.List) ArrayList(java.util.ArrayList) ForeignKey(org.hibernate.mapping.ForeignKey) DuplicateMappingException(org.hibernate.DuplicateMappingException) MappingException(org.hibernate.MappingException) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 7 with ForeignKey

use of org.hibernate.mapping.ForeignKey in project hibernate-orm by hibernate.

the class InFlightMetadataCollectorImpl method secondPassCompileForeignKeys.

private void secondPassCompileForeignKeys(MetadataBuildingContext buildingContext) {
    int uniqueInteger = 0;
    Set<ForeignKey> done = new HashSet<ForeignKey>();
    for (Table table : collectTableMappings()) {
        table.setUniqueInteger(uniqueInteger++);
        secondPassCompileForeignKeys(table, done, buildingContext);
    }
}
Also used : DenormalizedTable(org.hibernate.mapping.DenormalizedTable) Table(org.hibernate.mapping.Table) ForeignKey(org.hibernate.mapping.ForeignKey) HashSet(java.util.HashSet)

Example 8 with ForeignKey

use of org.hibernate.mapping.ForeignKey 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 9 with ForeignKey

use of org.hibernate.mapping.ForeignKey in project hibernate-orm by hibernate.

the class SchemaDropperImpl method applyConstraintDropping.

private void applyConstraintDropping(Namespace namespace, Metadata metadata, Formatter formatter, ExecutionOptions options, GenerationTarget... targets) {
    final Dialect dialect = metadata.getDatabase().getJdbcEnvironment().getDialect();
    if (!dialect.dropConstraints()) {
        return;
    }
    for (Table table : namespace.getTables()) {
        if (!table.isPhysicalTable()) {
            continue;
        }
        if (!schemaFilter.includeTable(table)) {
            continue;
        }
        final Iterator fks = table.getForeignKeyIterator();
        while (fks.hasNext()) {
            final ForeignKey foreignKey = (ForeignKey) fks.next();
            applySqlStrings(dialect.getForeignKeyExporter().getSqlDropStrings(foreignKey, metadata), formatter, options, targets);
        }
    }
}
Also used : Table(org.hibernate.mapping.Table) Dialect(org.hibernate.dialect.Dialect) Iterator(java.util.Iterator) ForeignKey(org.hibernate.mapping.ForeignKey)

Example 10 with ForeignKey

use of org.hibernate.mapping.ForeignKey in project hibernate-orm by hibernate.

the class DisabledForeignKeyTest method basicTests.

@Test
@TestForIssue(jiraKey = "HHH-9704")
public void basicTests() {
    StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder();
    StandardServiceRegistry standardRegistry = registryBuilder.build();
    try {
        final MetadataSources sources = new MetadataSources(standardRegistry);
        sources.addAnnotatedClass(ManyToManyOwner.class);
        sources.addAnnotatedClass(ManyToManyTarget.class);
        final MetadataImplementor metadata = (MetadataImplementor) sources.buildMetadata();
        metadata.validate();
        new SchemaExport().execute(EnumSet.of(TargetType.STDOUT), SchemaExport.Action.CREATE, metadata);
        int fkCount = 0;
        for (Table table : metadata.collectTableMappings()) {
            for (Map.Entry<ForeignKeyKey, ForeignKey> entry : table.getForeignKeys().entrySet()) {
                assertFalse("Creation for ForeignKey [" + entry.getKey() + "] was not disabled", entry.getValue().isCreationEnabled());
                fkCount++;
            }
        }
        // ultimately I want to actually create the ForeignKet reference, but simply disable its creation
        // via ForeignKet#disableCreation()
        assertEquals("Was expecting 4 FKs", 0, fkCount);
    } finally {
        StandardServiceRegistryBuilder.destroy(standardRegistry);
    }
}
Also used : ForeignKeyKey(org.hibernate.mapping.Table.ForeignKeyKey) Table(org.hibernate.mapping.Table) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) ForeignKey(org.hibernate.mapping.ForeignKey) Map(java.util.Map) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

ForeignKey (org.hibernate.mapping.ForeignKey)11 Iterator (java.util.Iterator)7 Table (org.hibernate.mapping.Table)5 PersistentClass (org.hibernate.mapping.PersistentClass)3 UniqueKey (org.hibernate.mapping.UniqueKey)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Identifier (org.hibernate.boot.model.naming.Identifier)2 Namespace (org.hibernate.boot.model.relational.Namespace)2 Column (org.hibernate.mapping.Column)2 Index (org.hibernate.mapping.Index)2 TestForIssue (org.hibernate.testing.TestForIssue)2 Map (java.util.Map)1 JoinColumn (javax.persistence.JoinColumn)1 UniqueConstraint (javax.persistence.UniqueConstraint)1 DuplicateMappingException (org.hibernate.DuplicateMappingException)1 MappingException (org.hibernate.MappingException)1 MetadataSources (org.hibernate.boot.MetadataSources)1