You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by nt...@apache.org on 2019/10/18 09:00:16 UTC

[cayenne] 03/03: CAY-2633 Modeler: attribute sorting logic in cgen can corrupt model

This is an automated email from the ASF dual-hosted git repository.

ntimofeev pushed a commit to branch STABLE-4.1
in repository https://gitbox.apache.org/repos/asf/cayenne.git

commit f8da476958835876d9b48f8b1122fa09ca640abe
Author: Nikita Timofeev <st...@gmail.com>
AuthorDate: Fri Oct 18 11:19:54 2019 +0300

    CAY-2633 Modeler: attribute sorting logic in cgen can corrupt model
    
    (cherry picked from commit 52f4cfdef7f78be2ffb242e555e6199acf66f286)
---
 RELEASE-NOTES.txt                                  |  1 +
 .../org/apache/cayenne/gen/EntityArtifact.java     | 23 ++++++++++++----------
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index 5a62f15..431b410 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -18,6 +18,7 @@ CAY-2627 Modeler: ObjRelationship creation dialog ignores delete rule
 CAY-2628 dbimport: unable to add several relationships to existing entity
 CAY-2631 Can no longer use "byte[]" as root of scalar SQLSelect
 CAY-2632 Modeler: issue saving cgen path for maven project
+CAY-2633 Modeler: attribute sorting logic in cgen can corrupt model
 
 ----------------------------------
 Release: 4.1.RC1
diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EntityArtifact.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EntityArtifact.java
index 9b19edf..fd4ca3c 100644
--- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EntityArtifact.java
+++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EntityArtifact.java
@@ -18,8 +18,9 @@
  ****************************************************************/
 package org.apache.cayenne.gen;
 
-import java.util.Map;
-import java.util.TreeMap;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
 
 import org.apache.cayenne.BaseDataObject;
 import org.apache.cayenne.map.ObjAttribute;
@@ -104,18 +105,20 @@ public class EntityArtifact implements Artifact {
     }
 
     private void sortAttributes() {
-        Map<String, ObjAttribute> attributeMap = new TreeMap<>(entity.getAttributeMap());
-        for(Map.Entry<String, ObjAttribute> entry : attributeMap.entrySet()) {
-            entity.removeAttribute(entry.getKey());
-            entity.addAttribute(entry.getValue());
+        List<ObjAttribute> objAttributes = new ArrayList<>(entity.getDeclaredAttributes());
+        objAttributes.sort(Comparator.comparing(ObjAttribute::getName));
+        for(ObjAttribute attribute : objAttributes) {
+            entity.removeAttribute(attribute.getName());
+            entity.addAttribute(attribute);
         }
     }
 
     private void sortRelationships() {
-        Map<String, ObjRelationship> relationshipMap = new TreeMap<>(entity.getRelationshipMap());
-        for(Map.Entry<String, ObjRelationship> entry : relationshipMap.entrySet()) {
-            entity.removeRelationship(entry.getKey());
-            entity.addRelationship(entry.getValue());
+        List<ObjRelationship> relationships = new ArrayList<>(entity.getDeclaredRelationships());
+        relationships.sort(Comparator.comparing(ObjRelationship::getName));
+        for(ObjRelationship relationship : relationships) {
+            entity.removeRelationship(relationship.getName());
+            entity.addRelationship(relationship);
         }
     }
 }