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();
}
}
}
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);
}
}
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);
}
}
}
}
}
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);
}
}
}
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);
}
}
Aggregations