Hey, guys,
it is possible that foreign key references non-primary key, but some candidate key. For example unique column.
But seems it was not taken into account in ForeignKey class.
And that is why we have a NPE in the case.
Could somebody review the patch which fixes the problem?

(see https://issues.jboss.org/browse/JBIDE-5461 for more details)
Thanks,
Dmitry Geraskov
Hibernate tools plugins developer.
### Eclipse Workspace Patch 1.0
#P Branch_3_2_5.ga
Index: src/org/hibernate/mapping/ForeignKey.java
===================================================================
--- src/org/hibernate/mapping/ForeignKey.java   (revision 17777)
+++ src/org/hibernate/mapping/ForeignKey.java   (working copy)
@@ -70,7 +70,19 @@
         * @param referencedTable
         */
        public void alignColumns() {
-               if ( isReferenceToPrimaryKey() ) alignColumns(referencedTable);
+               if ( isReferenceToPrimaryKey() ) {
+                       alignColumns(referencedTable);
+               } else {
+                       Iterator fkCols = getColumnIterator();
+                       while ( fkCols.hasNext() ) {
+                               Column fkColumn = (Column) fkCols.next();
+                               if (fkColumn.isFormula()) continue; 
+                               Column refColumn = 
referencedTable.getColumn(fkColumn);
+                               if (refColumn != null){
+                                       fkColumn.setLength( 
refColumn.getLength() );
+                               }                               
+                       }
+               }
        }
        
        private void alignColumns(Table referencedTable) {
@@ -134,7 +146,7 @@
 
        /** Does this foreignkey reference the primary key of the reference 
table */ 
        public boolean isReferenceToPrimaryKey() {
-               return referencedColumns.isEmpty();
+               return referencedColumns.isEmpty() && 
referencedTable.getPrimaryKey() != null;
        }
 
        public void addReferencedColumns(Iterator referencedColumnsIterator) {
_______________________________________________
hibernate-dev mailing list
hibernate-dev@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/hibernate-dev

Reply via email to