FileDocCategorySizeDatePackage
ForeignKey.javaAPI DocHibernate 3.2.55154Fri Jul 01 11:38:04 BST 2005org.hibernate.mapping

ForeignKey

public class ForeignKey extends Constraint
A foreign key constraint
author
Gavin King

Fields Summary
private Table
referencedTable
private String
referencedEntityName
private boolean
cascadeDeleteEnabled
private List
referencedColumns
Constructors Summary
Methods Summary
private voidaddReferencedColumn(Column column)

		if ( !referencedColumns.contains(column) ) referencedColumns.add(column);		
	
public voidaddReferencedColumns(java.util.Iterator referencedColumnsIterator)

		while ( referencedColumnsIterator.hasNext() ) {
			Selectable col = (Selectable) referencedColumnsIterator.next();
			if ( !col.isFormula() ) addReferencedColumn( (Column) col );
		}
	
public voidalignColumns()
Validates that columnspan of the foreignkey and the primarykey is the same. Furthermore it aligns the length of the underlying tables columns.

param
referencedTable

		if ( isReferenceToPrimaryKey() ) alignColumns(referencedTable);
	
private voidalignColumns(Table referencedTable)

		if ( referencedTable.getPrimaryKey().getColumnSpan()!=getColumnSpan() ) {
			StringBuffer sb = new StringBuffer();
			sb.append("Foreign key (")
                .append( getName() + ":")
				.append( getTable().getName() )
				.append(" [");
			appendColumns( sb, getColumnIterator() );
			sb.append("])")
				.append(") must have same number of columns as the referenced primary key (")
				.append( referencedTable.getName() )
				.append(" [");
			appendColumns( sb, referencedTable.getPrimaryKey().getColumnIterator() );
			sb.append("])");
			throw new MappingException( sb.toString() );
		}
		
		Iterator fkCols = getColumnIterator();
		Iterator pkCols = referencedTable.getPrimaryKey().getColumnIterator();
		while ( pkCols.hasNext() ) {
			( (Column) fkCols.next() ).setLength( ( (Column) pkCols.next() ).getLength() );
		}

	
private voidappendColumns(java.lang.StringBuffer buf, java.util.Iterator columns)

		while( columns.hasNext() ) {
			Column column = (Column) columns.next();
			buf.append( column.getName() );
			if ( columns.hasNext() ) buf.append(",");
		}
	
public java.util.ListgetReferencedColumns()
Returns the referenced columns if the foreignkey does not refer to the primary key

		return referencedColumns;		
	
public java.lang.StringgetReferencedEntityName()

		return referencedEntityName;
	
public TablegetReferencedTable()

		return referencedTable;
	
public booleanisCascadeDeleteEnabled()

		return cascadeDeleteEnabled;
	
public booleanisPhysicalConstraint()

		return referencedTable.isPhysicalTable() && 
				getTable().isPhysicalTable() && 
				!referencedTable.hasDenormalizedTables();
	
public booleanisReferenceToPrimaryKey()
Does this foreignkey reference the primary key of the reference table

		return referencedColumns.isEmpty();
	
public voidsetCascadeDeleteEnabled(boolean cascadeDeleteEnabled)

		this.cascadeDeleteEnabled = cascadeDeleteEnabled;
	
public voidsetReferencedEntityName(java.lang.String referencedEntityName)

		this.referencedEntityName = referencedEntityName;
	
public voidsetReferencedTable(Table referencedTable)

		//if( isReferenceToPrimaryKey() ) alignColumns(referencedTable); // TODO: possibly remove to allow more piecemal building of a foreignkey.  
		
		this.referencedTable = referencedTable;
	
public java.lang.StringsqlConstraintString(org.hibernate.dialect.Dialect dialect, java.lang.String constraintName, java.lang.String defaultCatalog, java.lang.String defaultSchema)

    
    
	          
		String[] cols = new String[ getColumnSpan() ];
		String[] refcols = new String[ getColumnSpan() ];
		int i=0;
		Iterator refiter = null;
		if(isReferenceToPrimaryKey() ) {
			refiter = referencedTable.getPrimaryKey().getColumnIterator();
		} 
		else {
			refiter = referencedColumns.iterator();
		}
		
		Iterator iter = getColumnIterator();
		while ( iter.hasNext() ) {
			cols[i] = ( (Column) iter.next() ).getQuotedName(dialect);
			refcols[i] = ( (Column) refiter.next() ).getQuotedName(dialect);
			i++;
		}
		String result = dialect.getAddForeignKeyConstraintString(
			constraintName, cols, referencedTable.getQualifiedName(dialect, defaultCatalog, defaultSchema), refcols, isReferenceToPrimaryKey()
		);
		return cascadeDeleteEnabled && dialect.supportsCascadeDelete() ? 
			result + " on delete cascade" : 
			result;
	
public java.lang.StringsqlDropString(org.hibernate.dialect.Dialect dialect, java.lang.String defaultCatalog, java.lang.String defaultSchema)

		return "alter table " + 
			getTable().getQualifiedName(dialect, defaultCatalog, defaultSchema) + 
			dialect.getDropForeignKeyString() + 
			getName();
	
public java.lang.StringtoString()

		if(!isReferenceToPrimaryKey() ) {
			StringBuffer result = new StringBuffer(getClass().getName() + '(" + getTable().getName() + getColumns() );
			result.append( " ref-columns:" + '(" + getReferencedColumns() );
			result.append( ") as " + getName() );
			return result.toString();
		} 
		else {
			return super.toString();
		}