Repository: cayenne
Updated Branches:
  refs/heads/master 87c9c8f4b -> bd1b109a9


CAY-2286 Filter out inherited ObjEntities from sync with DbEntity


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/bd1b109a
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/bd1b109a
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/bd1b109a

Branch: refs/heads/master
Commit: bd1b109a943307a83078399c7a4d6aa53631a065
Parents: 87c9c8f
Author: Nikita Timofeev <stari...@gmail.com>
Authored: Fri Apr 28 16:55:34 2017 +0300
Committer: Nikita Timofeev <stari...@gmail.com>
Committed: Fri Apr 28 16:55:34 2017 +0300

----------------------------------------------------------------------
 .../merge/token/model/AbstractToModelToken.java | 17 ++++++
 .../merge/token/model/AddColumnToModel.java     |  2 +-
 .../token/model/AddRelationshipToModel.java     |  2 +-
 docs/doc/src/main/resources/RELEASE-NOTES.txt   |  1 +
 .../cayenne/tools/DbImporterMojoTest.java       |  5 ++
 .../tools/dbimport/testInheritance-pom.xml      | 41 ++++++++++++++
 .../tools/dbimport/testInheritance.map.xml      | 38 +++++++++++++
 .../dbimport/testInheritance.map.xml-result     | 58 ++++++++++++++++++++
 .../cayenne/tools/dbimport/testInheritance.sql  | 35 ++++++++++++
 .../modeler/action/DbEntitySyncAction.java      | 17 ++++++
 10 files changed, 214 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/bd1b109a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AbstractToModelToken.java
----------------------------------------------------------------------
diff --git 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AbstractToModelToken.java
 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AbstractToModelToken.java
index a6544b2..5a6eb8e 100644
--- 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AbstractToModelToken.java
+++ 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AbstractToModelToken.java
@@ -19,6 +19,9 @@
 
 package org.apache.cayenne.dbsync.merge.token.model;
 
+import java.util.Collection;
+import java.util.Iterator;
+
 import org.apache.cayenne.dbsync.merge.context.MergeDirection;
 import org.apache.cayenne.dbsync.merge.token.AbstractMergerToken;
 import org.apache.cayenne.dbsync.merge.token.MergerToken;
@@ -85,6 +88,20 @@ public abstract class AbstractToModelToken extends 
AbstractMergerToken {
             return entity;
         }
 
+        /**
+         * @return ObjEntities mapped to current DbEntity excluding inherited
+         */
+        public Collection<ObjEntity> getMappedObjEntities() {
+            Collection<ObjEntity> entities = entity.mappedObjEntities();
+            Iterator<ObjEntity> iterator = entities.iterator();
+            while(iterator.hasNext()) {
+                if(iterator.next().getSuperEntity() != null) {
+                    iterator.remove();
+                }
+            }
+            return entities;
+        }
+
         public String getTokenValue() {
             return getEntity().getName();
         }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/bd1b109a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AddColumnToModel.java
----------------------------------------------------------------------
diff --git 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AddColumnToModel.java
 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AddColumnToModel.java
index 07ce947..af1a00c 100644
--- 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AddColumnToModel.java
+++ 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AddColumnToModel.java
@@ -46,7 +46,7 @@ public class AddColumnToModel extends 
AbstractToModelToken.EntityAndColumn {
     public void execute(MergerContext mergerContext) {
         getEntity().addAttribute(getColumn());
 
-        for (ObjEntity e : getEntity().mappedObjEntities()) {
+        for (ObjEntity e : getMappedObjEntities()) {
             
mergerContext.getEntityMergeSupport().synchronizeOnDbAttributeAdded(e, 
getColumn());
         }
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/bd1b109a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AddRelationshipToModel.java
----------------------------------------------------------------------
diff --git 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AddRelationshipToModel.java
 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AddRelationshipToModel.java
index 8647485..b34e017 100644
--- 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AddRelationshipToModel.java
+++ 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/token/model/AddRelationshipToModel.java
@@ -71,7 +71,7 @@ public class AddRelationshipToModel extends 
AbstractToModelToken.Entity {
         }
 
         getEntity().addRelationship(relationship);
-        for (ObjEntity e : getEntity().mappedObjEntities()) {
+        for (ObjEntity e : getMappedObjEntities()) {
             
context.getEntityMergeSupport().synchronizeOnDbRelationshipAdded(e, 
relationship);
         }
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/bd1b109a/docs/doc/src/main/resources/RELEASE-NOTES.txt
----------------------------------------------------------------------
diff --git a/docs/doc/src/main/resources/RELEASE-NOTES.txt 
b/docs/doc/src/main/resources/RELEASE-NOTES.txt
index 674903a..40c9097 100644
--- a/docs/doc/src/main/resources/RELEASE-NOTES.txt
+++ b/docs/doc/src/main/resources/RELEASE-NOTES.txt
@@ -56,6 +56,7 @@ CAY-2276 PrePersist listener registered as PostPersist in 
LifecycleCallbackRegis
 CAY-2279 cdbimport: skip PK comparison for VIEWs
 CAY-2281 ObjEntity attribute overrides are never deleted
 CAY-2284 Expression likeIgnoreCase can't handle unicode chars in in-memory 
evaluation
+CAY-2286 Filter out inherited ObjEntities from sync with DbEntity
 
 ----------------------------------
 Release: 4.0.M5

http://git-wip-us.apache.org/repos/asf/cayenne/blob/bd1b109a/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
----------------------------------------------------------------------
diff --git 
a/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
 
b/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
index 63a349d..5d446fd 100644
--- 
a/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
+++ 
b/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
@@ -257,6 +257,11 @@ public class DbImporterMojoTest extends 
AbstractMojoTestCase {
     }
 
     @Test
+    public void testInheritance() throws Exception {
+        test("testInheritance");
+    }
+
+    @Test
     public void testFilteringConfig() throws Exception {
         DbImporterMojo cdbImport = getCdbImport("config/pom-01.xml");
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/bd1b109a/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance-pom.xml
----------------------------------------------------------------------
diff --git 
a/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance-pom.xml
 
b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance-pom.xml
new file mode 100644
index 0000000..37c18d4
--- /dev/null
+++ 
b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance-pom.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+  ~   Licensed to the Apache Software Foundation (ASF) under one
+  ~  or more contributor license agreements.  See the NOTICE file
+  ~  distributed with this work for additional information
+  ~  regarding copyright ownership.  The ASF licenses this file
+  ~  to you under the Apache License, Version 2.0 (the
+  ~  "License"); you may not use this file except in compliance
+  ~  with the License.  You may obtain a copy of the License at
+  ~
+  ~    http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~  Unless required by applicable law or agreed to in writing,
+  ~  software distributed under the License is distributed on an
+  ~  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~  KIND, either express or implied.  See the License for the
+  ~  specific language governing permissions and limitations
+  ~  under the License.
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
+       http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+
+       <name>DbImporterMojo Test1</name>       
+
+       <build>
+               <plugins>
+                       <plugin>
+                               <artifactId>cayenne-maven-plugin</artifactId>
+                               <configuration>
+                                       
<map>target/test-classes/org/apache/cayenne/tools/dbimport/testInheritance.map.xml</map>
+                                       <dataSource>
+                                               
<driver>org.apache.derby.jdbc.EmbeddedDriver</driver>
+                                               
<url>jdbc:derby:memory:DbImporterMojoTest;create=true</url>
+                                       </dataSource>
+                               </configuration>
+                       </plugin>
+               </plugins>
+       </build>
+
+</project>

http://git-wip-us.apache.org/repos/asf/cayenne/blob/bd1b109a/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance.map.xml
----------------------------------------------------------------------
diff --git 
a/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance.map.xml
 
b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance.map.xml
new file mode 100644
index 0000000..b849751
--- /dev/null
+++ 
b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance.map.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+  ~   Licensed to the Apache Software Foundation (ASF) under one
+  ~  or more contributor license agreements.  See the NOTICE file
+  ~  distributed with this work for additional information
+  ~  regarding copyright ownership.  The ASF licenses this file
+  ~  to you under the Apache License, Version 2.0 (the
+  ~  "License"); you may not use this file except in compliance
+  ~  with the License.  You may obtain a copy of the License at
+  ~
+  ~    http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~  Unless required by applicable law or agreed to in writing,
+  ~  software distributed under the License is distributed on an
+  ~  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~  KIND, either express or implied.  See the License for the
+  ~  specific language governing permissions and limitations
+  ~  under the License.
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
+<data-map xmlns="http://cayenne.apache.org/schema/9/modelMap";
+          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+          xsi:schemaLocation="http://cayenne.apache.org/schema/9/modelMap 
http://cayenne.apache.org/schema/9/modelMap.xsd";
+          project-version="9">
+
+    <db-entity name="CHILD" schema="SCHEMA_01">
+        <db-attribute name="id" type="INTEGER" isPrimaryKey="true" 
isMandatory="true" length="10"/>
+        <db-attribute name="parent_id" type="INTEGER" length="10"/>
+    </db-entity>
+    <db-entity name="PARENT" schema="SCHEMA_01">
+        <db-attribute name="id" type="INTEGER" isPrimaryKey="true" 
isMandatory="true" length="10"/>
+    </db-entity>
+    <obj-entity name="Child" className="Child" dbEntityName="CHILD">
+    </obj-entity>
+    <obj-entity name="Parent" className="Parent" dbEntityName="PARENT">
+    </obj-entity>
+    <obj-entity name="ParentSub" className="ParentSub" dbEntityName="PARENT" 
superEntityName="Parent">
+    </obj-entity>
+</data-map>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cayenne/blob/bd1b109a/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance.map.xml-result
----------------------------------------------------------------------
diff --git 
a/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance.map.xml-result
 
b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance.map.xml-result
new file mode 100644
index 0000000..7e54338
--- /dev/null
+++ 
b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance.map.xml-result
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+  ~   Licensed to the Apache Software Foundation (ASF) under one
+  ~  or more contributor license agreements.  See the NOTICE file
+  ~  distributed with this work for additional information
+  ~  regarding copyright ownership.  The ASF licenses this file
+  ~  to you under the Apache License, Version 2.0 (the
+  ~  "License"); you may not use this file except in compliance
+  ~  with the License.  You may obtain a copy of the License at
+  ~
+  ~    http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~  Unless required by applicable law or agreed to in writing,
+  ~  software distributed under the License is distributed on an
+  ~  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~  KIND, either express or implied.  See the License for the
+  ~  specific language governing permissions and limitations
+  ~  under the License.
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
+<data-map xmlns="http://cayenne.apache.org/schema/9/modelMap";
+          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+          xsi:schemaLocation="http://cayenne.apache.org/schema/9/modelMap 
http://cayenne.apache.org/schema/9/modelMap.xsd";
+          project-version="9">
+
+    <db-entity name="CHILD" schema="SCHEMA_01">
+        <db-attribute name="id" type="INTEGER" isPrimaryKey="true" 
isMandatory="true" length="10"/>
+        <db-attribute name="parent_id" type="INTEGER" length="10"/>
+    </db-entity>
+    <db-entity name="PARENT" schema="SCHEMA_01">
+        <db-attribute name="NAME" type="VARCHAR" length="255"/>
+        <db-attribute name="id" type="INTEGER" isPrimaryKey="true" 
isMandatory="true" length="10"/>
+    </db-entity>
+    <obj-entity name="Child" className="Child" dbEntityName="CHILD">
+    </obj-entity>
+    <obj-entity name="Parent" className="Parent" dbEntityName="PARENT">
+        <obj-attribute name="name" type="java.lang.String" 
db-attribute-path="NAME"/>
+    </obj-entity>
+    <obj-entity name="ParentSub" superEntityName="Parent" 
className="ParentSub">
+    </obj-entity>
+    <db-relationship name="parent" source="CHILD" target="PARENT" 
toMany="false">
+        <db-attribute-pair source="parent_id" target="id"/>
+    </db-relationship>
+    <db-relationship name="children" source="PARENT" target="CHILD" 
toMany="true">
+        <db-attribute-pair source="id" target="parent_id"/>
+    </db-relationship>
+    <obj-relationship name="parent" source="Child" target="Parent" 
deleteRule="Nullify" db-relationship-path="parent"/>
+    <obj-relationship name="parent1" source="Child" target="ParentSub" 
deleteRule="Nullify" db-relationship-path="parent"/>
+    <obj-relationship name="children" source="Parent" target="Child" 
deleteRule="Deny" db-relationship-path="children"/>
+</data-map>
+
+
+
+
+
+
+
+
+

http://git-wip-us.apache.org/repos/asf/cayenne/blob/bd1b109a/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance.sql
----------------------------------------------------------------------
diff --git 
a/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance.sql
 
b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance.sql
new file mode 100644
index 0000000..595da3e
--- /dev/null
+++ 
b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testInheritance.sql
@@ -0,0 +1,35 @@
+--  Licensed to the Apache Software Foundation (ASF) under one
+--  or more contributor license agreements.  See the NOTICE file
+--  distributed with this work for additional information
+-- regarding copyright ownership.  The ASF licenses this file
+--  to you under the Apache License, Version 2.0 (the
+--  "License"); you may not use this file except in compliance
+--  with the License.  You may obtain a copy of the License at
+--
+--    http://www.apache.org/licenses/LICENSE-2.0
+--
+--  Unless required by applicable law or agreed to in writing,
+--  software distributed under the License is distributed on an
+--  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+--  KIND, either express or implied.  See the License for the
+--  specific language governing permissions and limitations
+--  under the License.
+
+CREATE SCHEMA schema_01;
+SET SCHEMA schema_01;
+
+CREATE TABLE schema_01.parent (
+    id INTEGER NOT NULL,
+    name VARCHAR(255),
+    PRIMARY KEY (id)
+);
+
+CREATE TABLE schema_01.child (
+    id INTEGER,
+    parent_id INTEGER,
+    PRIMARY KEY (id)
+);
+
+ALTER TABLE schema_01.child
+    ADD FOREIGN KEY (parent_id)
+    REFERENCES schema_01.parent (id);
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cayenne/blob/bd1b109a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/DbEntitySyncAction.java
----------------------------------------------------------------------
diff --git 
a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/DbEntitySyncAction.java
 
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/DbEntitySyncAction.java
index 9b4cb30..3b1ef7f 100644
--- 
a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/DbEntitySyncAction.java
+++ 
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/DbEntitySyncAction.java
@@ -35,6 +35,7 @@ import org.apache.cayenne.modeler.util.CayenneAction;
 
 import java.awt.event.ActionEvent;
 import java.util.Collection;
+import java.util.Iterator;
 
 /**
  * Action that synchronizes all ObjEntities with the current state of the
@@ -85,6 +86,8 @@ public class DbEntitySyncAction extends CayenneAction {
                        DbEntitySyncUndoableEdit undoableEdit = new 
DbEntitySyncUndoableEdit((DataChannelDescriptor) mediator
                                        .getProject().getRootNode(), 
mediator.getCurrentDataMap());
 
+                       // filter out inherited entities, as we need to add 
attributes only to the roots
+                       filterInheritedEntities(entities);
 
                        for(ObjEntity entity : entities) {
 
@@ -111,6 +114,20 @@ public class DbEntitySyncAction extends CayenneAction {
                }
        }
 
+       /**
+        * This method works only for case when all inherited entities bound to 
same DbEntity
+        * if this will ever change some additional checks should be performed.
+        */
+       private void filterInheritedEntities(Collection<ObjEntity> entities) {
+               // entities.removeIf(c -> c.getSuperEntity() != null);
+               Iterator<ObjEntity> it = entities.iterator();
+               while(it.hasNext()) {
+                       if(it.next().getSuperEntity() != null) {
+                               it.remove();
+                       }
+               }
+       }
+
        static class PreserveRelationshipNameGenerator extends 
DefaultObjectNameGenerator {
 
                @Override

Reply via email to