KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > mapping > ForeignKey


1 //$Id: ForeignKey.java,v 1.14 2005/07/01 16:38:02 maxcsaucdk Exp $
2
package org.hibernate.mapping;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7
8 import org.hibernate.MappingException;
9 import org.hibernate.dialect.Dialect;
10
11 /**
12  * A foreign key constraint
13  * @author Gavin King
14  */

15 public class ForeignKey extends Constraint {
16
17     private Table referencedTable;
18     private String JavaDoc referencedEntityName;
19     private boolean cascadeDeleteEnabled;
20     private List referencedColumns = new ArrayList JavaDoc();
21     
22     public String JavaDoc sqlConstraintString(Dialect dialect, String JavaDoc constraintName, String JavaDoc defaultCatalog, String JavaDoc defaultSchema) {
23         String JavaDoc[] cols = new String JavaDoc[ getColumnSpan() ];
24         String JavaDoc[] refcols = new String JavaDoc[ getColumnSpan() ];
25         int i=0;
26         Iterator JavaDoc refiter = null;
27         if(isReferenceToPrimaryKey() ) {
28             refiter = referencedTable.getPrimaryKey().getColumnIterator();
29         }
30         else {
31             refiter = referencedColumns.iterator();
32         }
33         
34         Iterator JavaDoc 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 JavaDoc 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 JavaDoc buf, Iterator JavaDoc 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         //if( isReferenceToPrimaryKey() ) alignColumns(referencedTable); // TODO: possibly remove to allow more piecemal building of a foreignkey.
62

63         this.referencedTable = referencedTable;
64     }
65
66     /**
67      * Validates that columnspan of the foreignkey and the primarykey is the same.
68      *
69      * Furthermore it aligns the length of the underlying tables columns.
70      * @param referencedTable
71      */

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 JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc fkCols = getColumnIterator();
94         Iterator JavaDoc pkCols = referencedTable.getPrimaryKey().getColumnIterator();
95         while ( pkCols.hasNext() ) {
96             ( (Column) fkCols.next() ).setLength( ( (Column) pkCols.next() ).getLength() );
97         }
98
99     }
100
101     public String JavaDoc getReferencedEntityName() {
102         return referencedEntityName;
103     }
104
105     public void setReferencedEntityName(String JavaDoc referencedEntityName) {
106         this.referencedEntityName = referencedEntityName;
107     }
108
109     public String JavaDoc sqlDropString(Dialect dialect, String JavaDoc defaultCatalog, String JavaDoc 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     /** Returns the referenced columns if the foreignkey does not refer to the primary key */
131     public List getReferencedColumns() {
132         return referencedColumns;
133     }
134
135     /** Does this foreignkey reference the primary key of the reference table */
136     public boolean isReferenceToPrimaryKey() {
137         return referencedColumns.isEmpty();
138     }
139
140     public void addReferencedColumns(Iterator JavaDoc 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 JavaDoc toString() {
152         if(!isReferenceToPrimaryKey() ) {
153             StringBuffer JavaDoc result = new StringBuffer JavaDoc(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