Java Code Examples for org.hibernate.mapping.Table#getForeignKeyIterator()

The following examples show how to use org.hibernate.mapping.Table#getForeignKeyIterator() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
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() ) {
				boolean existingForeignKeyFound = false;
				if ( tableInformation != null ) {
					existingForeignKeyFound = checkForExistingForeignKey(
							foreignKey,
							tableInformation
					);
				}
				if ( !existingForeignKeyFound ) {
					// 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
					);
				}
			}
		}
	}
}
 
Example 2
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
			);
		}
	}
}
 
Example 3
protected void secondPassCompileForeignKeys(Table table, Set done) throws MappingException {

		table.createForeignKeys();

		Iterator iter = table.getForeignKeyIterator();
		while ( iter.hasNext() ) {

			ForeignKey fk = (ForeignKey) iter.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"
						);
				}
				if ( log.isDebugEnabled() ) {
					log.debug( "resolving reference to class: " + referencedEntityName );
				}
				PersistentClass referencedClass = (PersistentClass) classes.get( 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 );
				}
				fk.setReferencedTable( referencedClass.getTable() );
				fk.alignColumns();
			}
		}
	}
 
Example 4
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();
		}
	}
}