You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2010/03/28 22:50:24 UTC

svn commit: r928493 - in /cayenne/main/trunk: docs/doc/src/main/resources/RELEASE-NOTES.txt framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/autorelationship/InferRelationshipsControllerBase.java

Author: aadamchik
Date: Sun Mar 28 20:50:23 2010
New Revision: 928493

URL: http://svn.apache.org/viewvc?rev=928493&view=rev
Log:
CAY-1405  Exception when attempting to "Infer Relationships"

Fixing NPE per Jira and also adding some trivial string comparision optimizations

Modified:
    cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/autorelationship/InferRelationshipsControllerBase.java

Modified: cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt?rev=928493&r1=928492&r2=928493&view=diff
==============================================================================
--- cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt (original)
+++ cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt Sun Mar 28 20:50:23 2010
@@ -61,6 +61,7 @@ CAY-1397 ROP: Missing the COMMIT state w
 CAY-1400 lazyInitParentDomainName will become null when using Child Context
 CAY-1401 Cache group removed from modeler query
 CAY-1404 ClassCastException during ObjectDetachOperation
+CAY-1405 Exception when attempting to "Infer Relationships"
 CAY-1409 Pkgenerator is accessed needlessly for join tables with own database generated PK
 
 ----------------------------------

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/autorelationship/InferRelationshipsControllerBase.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/autorelationship/InferRelationshipsControllerBase.java?rev=928493&r1=928492&r2=928493&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/autorelationship/InferRelationshipsControllerBase.java (original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/autorelationship/InferRelationshipsControllerBase.java Sun Mar 28 20:50:23 2010
@@ -20,8 +20,8 @@ package org.apache.cayenne.modeler.dialo
 
 import java.awt.Component;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
@@ -71,27 +71,38 @@ public class InferRelationshipsControlle
     }
 
     protected void createRelationships(DbEntity entity) {
-        Collection<DbAttribute> attr = entity.getAttributes();
 
-        for (DbAttribute attribute : attr) {
+        for (DbAttribute attribute : entity.getAttributes()) {
 
-            for (DbEntity myEntity : entities) {
-                if (attribute.getName().equalsIgnoreCase(myEntity.getName() + "_ID")
+            String name = attribute.getName();
+            if (name.length() < 4) {
+                continue;
+            }
+
+            if (!name.substring(name.length() - 3, name.length()).equalsIgnoreCase("_ID")) {
+                continue;
+            }
+
+            String baseName = name.substring(0, name.length() - 3);
+            for (DbEntity targetEntity : entities) {
+                // TODO: should we handle relationships to self??
+                if (targetEntity == entity) {
+                    continue;
+                }
+
+                if (baseName.equalsIgnoreCase(targetEntity.getName())
                         && !attribute.isPrimaryKey()
-                        && !myEntity.getAttributes().isEmpty()
-                        && myEntity != entity) {
+                        && !targetEntity.getAttributes().isEmpty()) {
 
                     if (!attribute.isForeignKey()) {
                         InferredRelationship myir = new InferredRelationship();
                         myir.setSource(entity);
-                        myir.setTarget(myEntity);
+                        myir.setTarget(targetEntity);
                         inferredRelationships.add(myir);
                     }
-
-                    createReversRelationship(myEntity, entity);
+                    createReversRelationship(targetEntity, entity);
                 }
             }
-
         }
     }
 
@@ -153,16 +164,35 @@ public class InferRelationshipsControlle
     }
 
     protected void createJoins() {
-        for (InferredRelationship inferred : inferredRelationships) {
-            DbAttribute join = getJoinAttribute(inferred.getSource(), inferred
-                    .getTarget());
-            inferred.setJoinSource(join);
-            if (join.isPrimaryKey()) {
+        Iterator<InferredRelationship> it = inferredRelationships.iterator();
+        while (it.hasNext()) {
+            InferredRelationship inferred = it.next();
+
+            DbAttribute src = getJoinAttribute(inferred.getSource(), inferred.getTarget());
+            if (src == null) {
+                // TODO: andrus 03/28/2010 this is pretty inefficient I guess... We should
+                // check for this condition earlier. See CAY-1405 for the map that caused
+                // this issue
+                it.remove();
+                continue;
+            }
+
+            DbAttribute target = getJoinAttribute(inferred.getTarget(), inferred
+                    .getSource());
+            if (target == null) {
+                // TODO: andrus 03/28/2010 this is pretty inefficient I guess... We should
+                // check for this condition earlier. See CAY-1405 for the map that caused
+                // this issue
+                it.remove();
+                continue;
+            }
+
+            inferred.setJoinSource(src);
+            if (src.isPrimaryKey()) {
                 inferred.setToMany(true);
             }
 
-            inferred.setJoinTarget(getJoinAttribute(inferred.getTarget(), inferred
-                    .getSource()));
+            inferred.setJoinTarget(target);
         }
     }