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

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


The following commit(s) were added to refs/heads/STABLE-4.2 by this push:
     new 502362747 CAY-2800 Modeler saves map.xml files with schema ordering 
error
502362747 is described below

commit 50236274793f3d42fd95fa5220779a838c0e49b8
Author: Nikita Timofeev <stari...@gmail.com>
AuthorDate: Fri Mar 17 18:09:04 2023 +0300

    CAY-2800 Modeler saves map.xml files with schema ordering error
---
 RELEASE-NOTES.txt                                  |  1 +
 .../java/org/apache/cayenne/map/ObjEntity.java     | 15 ++++++++++++-
 .../java/org/apache/cayenne/map/ObjEntityTest.java | 26 ++++++++++++++++++++++
 3 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index 5321c62d8..7a8f439be 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -24,6 +24,7 @@ CAY-2790 EOModel import: NPE importing EOFetchSpecification
 CAY-2792 Fix Insertion Order For Reflexive DataObjects
 CAY-2794 Fix Incorrect JavaType for Vertical-Inheritance Attributes
 CAY-2796 Cayenne custom json formatter produces invalid json
+CAY-2800 Modeler saves map.xml files with schema ordering error
 CAY-2801 Incorrect equals() implementation in IdGenerationMarker could cause 
data missing in the commit
 
 ----------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/map/ObjEntity.java 
b/cayenne-server/src/main/java/org/apache/cayenne/map/ObjEntity.java
index 2ffaabf1c..d0d2500b1 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/map/ObjEntity.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/map/ObjEntity.java
@@ -149,8 +149,21 @@ public class ObjEntity extends Entity implements 
ObjEntityListener, Configuratio
             encoder.start("qualifier").nested(qualifier, delegate).end();
         }
 
+        // divide attributes by type
+        TreeMap<String, Attribute> embAttributes = new TreeMap<>();
+        TreeMap<String, Attribute> objAttributes = new TreeMap<>();
+
+        attributes.forEach((key, value) -> {
+            if (value instanceof EmbeddedAttribute) {
+                embAttributes.put(key, value);
+            } else {
+                objAttributes.put(key, value);
+            }
+        });
+
         // store attributes
-        encoder.nested(new TreeMap<>(attributes), delegate);
+        encoder.nested(embAttributes, delegate);
+        encoder.nested(objAttributes, delegate);
 
         for (Map.Entry<String, String> override : 
attributeOverrides.entrySet()) {
             encoder.start("attribute-override")
diff --git 
a/cayenne-server/src/test/java/org/apache/cayenne/map/ObjEntityTest.java 
b/cayenne-server/src/test/java/org/apache/cayenne/map/ObjEntityTest.java
index 920eb8b67..4d53a311b 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/map/ObjEntityTest.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/map/ObjEntityTest.java
@@ -55,6 +55,32 @@ public class ObjEntityTest {
                 "</obj-entity>" + ls, out.toString());
     }
 
+    @Test
+    public void testAttributeOrder() {
+        ObjEntity entity = new ObjEntity("X");
+        entity.setClassName("org.example.Xc");
+
+        entity.addAttribute(new ObjAttribute("a2", "java.lang.String", 
entity));
+        entity.addAttribute(new ObjAttribute("a1", "int", entity));
+
+        entity.addAttribute(new EmbeddedAttribute("a3", "long", entity));
+
+        // relationships are saved outside the entity, so should be ignored in 
this test
+        entity.addRelationship(new ObjRelationship("r1"));
+
+        StringWriter out = new StringWriter();
+        XMLEncoder encoder = new XMLEncoder(new PrintWriter(out));
+        entity.encodeAsXML(encoder, new EncoderDummyVisitor());
+
+        String ls = System.lineSeparator();
+
+        assertEquals("<obj-entity name=\"X\" className=\"org.example.Xc\">" + 
ls +
+                "<embedded-attribute name=\"a3\" type=\"long\"/>" + ls +
+                "<obj-attribute name=\"a1\" type=\"int\"/>" + ls +
+                "<obj-attribute name=\"a2\" type=\"java.lang.String\"/>" + ls +
+                "</obj-entity>" + ls, out.toString());
+    }
+
     private class EncoderDummyVisitor extends 
BaseConfigurationNodeVisitor<Object> {
         @Override
         public Object visitObjEntity(ObjEntity entity) {

Reply via email to