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

The following examples show how to use org.hibernate.mapping.Table#createForeignKeys() . 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
Source File: Configuration.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 2
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
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();
		}
	}
}