You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by "Andrus Adamchik (Jira)" <ji...@apache.org> on 2022/11/17 17:07:00 UTC

[jira] [Commented] (CAY-2777) Reverse relationship is not set with single table inheritance

    [ https://issues.apache.org/jira/browse/CAY-2777?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17635457#comment-17635457 ] 

Andrus Adamchik commented on CAY-2777:
--------------------------------------

I just tried a direct fix (see below). It fixed the immediate problem, but uncovered another unrelated problem with inheritance mapping - after "parent.addToChildren(child)" call, on commit two identical records are inserted in the join table T11, causing duplicate key exception. 

So we need to create real unit tests and make sure this case works end-to-end.

{noformat}
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/map/ObjRelationship.java b/cayenne-server/src/main/java/org/apache/cayenne/map/ObjRelationship.java
index ed4ecd18f..cb35e7bb4 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/map/ObjRelationship.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/map/ObjRelationship.java
@@ -193,11 +193,12 @@ public class ObjRelationship extends Relationship implements ConfigurationNode {
             return null;
         }
 
-        Entity source = getSourceEntity();
+        ObjEntity source = getSourceEntity();
 
         for (ObjRelationship relationship : target.getRelationships()) {
 
-            if (relationship.getTargetEntity() != source) {
+            ObjEntity maybeSameSource = relationship.getTargetEntity();
+            if (maybeSameSource != source && !source.isSubentityOf(maybeSameSource)) {
                 continue;
             }
 {noformat}

> Reverse relationship is not set with single table inheritance
> -------------------------------------------------------------
>
>                 Key: CAY-2777
>                 URL: https://issues.apache.org/jira/browse/CAY-2777
>             Project: Cayenne
>          Issue Type: Bug
>    Affects Versions: 4.2.RC1
>            Reporter: Andrus Adamchik
>            Priority: Minor
>             Fix For: 4.2
>
>
> Given the model:
> {noformat}
> // a table with a many to many to self
> T1: id, type
> T11: child_id, parent_id; 
> // Base ObjEntity
> O1: 
>   - flat rel "parents" mapped as "db:parents.parent"
> // Sub ObjEntity 1
> O1S1:
>   - flat rel "children" mapped as "db:children.child"
> // Sub ObjEntity 2
> O1S2:
> {noformat}
> {noformat}
> O1S1 parent = ...
> O1S2 child = ...
> // THE PROBLEM IS HERE
> // This fails to set "parents" relationship of O1S2
> parent.addToChildren(child);
> {noformat}
> The culprit seems to be "ObjRelationship.getReverseRelationship()", specifically this check here
> {noformat}
> for (ObjRelationship relationship : target.getRelationships()) {
>             // TODO: this should be replaced with inheritance-aware code. 
>             // "relationship.getTargetEntity()" should be allowed to be a 
>             // super entity of source
>             if (relationship.getTargetEntity() != source) {
>                 continue;
>             }
> {noformat}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)