1 package org.hibernate.mapping; 3 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 8 import org.hibernate.MappingException; 9 import org.hibernate.dialect.Dialect; 10 11 15 public class ForeignKey extends Constraint { 16 17 private Table referencedTable; 18 private String referencedEntityName; 19 private boolean cascadeDeleteEnabled; 20 private List referencedColumns = new ArrayList (); 21 22 public String sqlConstraintString(Dialect dialect, String constraintName, String defaultCatalog, String defaultSchema) { 23 String [] cols = new String [ getColumnSpan() ]; 24 String [] refcols = new String [ getColumnSpan() ]; 25 int i=0; 26 Iterator refiter = null; 27 if(isReferenceToPrimaryKey() ) { 28 refiter = referencedTable.getPrimaryKey().getColumnIterator(); 29 } 30 else { 31 refiter = referencedColumns.iterator(); 32 } 33 34 Iterator iter = getColumnIterator(); 35 while ( iter.hasNext() ) { 36 cols[i] = ( (Column) iter.next() ).getQuotedName(dialect); 37 refcols[i] = ( (Column) refiter.next() ).getQuotedName(dialect); 38 i++; 39 } 40 String result = dialect.getAddForeignKeyConstraintString( 41 constraintName, cols, referencedTable.getQualifiedName(dialect, defaultCatalog, defaultSchema), refcols, isReferenceToPrimaryKey() 42 ); 43 return cascadeDeleteEnabled && dialect.supportsCascadeDelete() ? 44 result + " on delete cascade" : 45 result; 46 } 47 48 public Table getReferencedTable() { 49 return referencedTable; 50 } 51 52 private void appendColumns(StringBuffer buf, Iterator columns) { 53 while( columns.hasNext() ) { 54 Column column = (Column) columns.next(); 55 buf.append( column.getName() ); 56 if ( columns.hasNext() ) buf.append(","); 57 } 58 } 59 60 public void setReferencedTable(Table referencedTable) throws MappingException { 61 63 this.referencedTable = referencedTable; 64 } 65 66 72 public void alignColumns() { 73 if ( isReferenceToPrimaryKey() ) alignColumns(referencedTable); 74 } 75 76 private void alignColumns(Table referencedTable) { 77 if ( referencedTable.getPrimaryKey().getColumnSpan()!=getColumnSpan() ) { 78 StringBuffer sb = new StringBuffer (); 79 sb.append("Foreign key (") 80 .append( getName() + ":") 81 .append( getTable().getName() ) 82 .append(" ["); 83 appendColumns( sb, getColumnIterator() ); 84 sb.append("])") 85 .append(") must have same number of columns as the referenced primary key (") 86 .append( referencedTable.getName() ) 87 .append(" ["); 88 appendColumns( sb, referencedTable.getPrimaryKey().getColumnIterator() ); 89 sb.append("])"); 90 throw new MappingException( sb.toString() ); 91 } 92 93 Iterator fkCols = getColumnIterator(); 94 Iterator pkCols = referencedTable.getPrimaryKey().getColumnIterator(); 95 while ( pkCols.hasNext() ) { 96 ( (Column) fkCols.next() ).setLength( ( (Column) pkCols.next() ).getLength() ); 97 } 98 99 } 100 101 public String getReferencedEntityName() { 102 return referencedEntityName; 103 } 104 105 public void setReferencedEntityName(String referencedEntityName) { 106 this.referencedEntityName = referencedEntityName; 107 } 108 109 public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) { 110 return "alter table " + 111 getTable().getQualifiedName(dialect, defaultCatalog, defaultSchema) + 112 dialect.getDropForeignKeyString() + 113 getName(); 114 } 115 116 public boolean isCascadeDeleteEnabled() { 117 return cascadeDeleteEnabled; 118 } 119 120 public void setCascadeDeleteEnabled(boolean cascadeDeleteEnabled) { 121 this.cascadeDeleteEnabled = cascadeDeleteEnabled; 122 } 123 124 public boolean isPhysicalConstraint() { 125 return referencedTable.isPhysicalTable() && 126 getTable().isPhysicalTable() && 127 !referencedTable.hasDenormalizedTables(); 128 } 129 130 131 public List getReferencedColumns() { 132 return referencedColumns; 133 } 134 135 136 public boolean isReferenceToPrimaryKey() { 137 return referencedColumns.isEmpty(); 138 } 139 140 public void addReferencedColumns(Iterator referencedColumnsIterator) { 141 while ( referencedColumnsIterator.hasNext() ) { 142 Selectable col = (Selectable) referencedColumnsIterator.next(); 143 if ( !col.isFormula() ) addReferencedColumn( (Column) col ); 144 } 145 } 146 147 private void addReferencedColumn(Column column) { 148 if ( !referencedColumns.contains(column) ) referencedColumns.add(column); 149 } 150 151 public String toString() { 152 if(!isReferenceToPrimaryKey() ) { 153 StringBuffer result = new StringBuffer (getClass().getName() + '(' + getTable().getName() + getColumns() ); 154 result.append( " ref-columns:" + '(' + getReferencedColumns() ); 155 result.append( ") as " + getName() ); 156 return result.toString(); 157 } 158 else { 159 return super.toString(); 160 } 161 162 } 163 } 164 | Popular Tags |