Announcement Announcement Module
Collapse
No announcement yet.
Spring Data Graph: Why only one relationship of a type between two nodes? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data Graph: Why only one relationship of a type between two nodes?

    Hello Graphistas

    I just had to learn the hard way that SDG does not allow having two relationships of the same type between the same two nodes. Is there any compelling reason why this restriction has been introduced? It makes the representation of our model unnecessarily hard.

    In our domain, we model data flows between objects. A user can create different flow models where he can put different objects and connects them with flow relationships. I.e.:
    Flow model 1 defines the flow:
    a --> b --> c

    Flow model 2 defines the flow:
    a --> b --> x --> c

    The resulting graph could be e.g.:
    https://chart.googleapis.com/chart?c...22fmId=f2%22]}

    In this graph model, there are now two "flows_to" relationships between the nodes a and b, once for the flow model 1 and once for the flow model 2. Our approach was to add the id of the flow model as a property "flowModelId" (fmId in the graph above) to the "flows_to" relationship. To recreate the flow in flow model 1, we'd traverse all flow_to relationships that have the "flowModelId" set to the appropriate flow model. This is a valid reason, having two relationships of the same type between the same two nodes, isn't it?

    Any chance to remove this restriction?


    Best regards,
    James

  • #2
    Yes there is a compelling reason for that.

    As SDG manages relationships it has to deal with things like the following:
    * for single relationship fields, on setting the field to a new value, the previous relationship has to be removed
    * for 1:n relationships (managed set) it has to do delta analysis of the existing and new/removed relationships.

    If we would allow multiple relationship of the same type to another entity this would no longer be possible.

    What I could think of is to add an attribute to @RelatedTo that allows you to skip those mechanisms (you would then responsible for managing the relationship creation and deletion on your own as you would probably take other things (like rel-properties into account) when updating relationships.

    I suggest that we could have @RelatedTo(unmanaged=true) which would cause,
    * no deletion of existing relationships
    * an error when multiple relationships to another entity exist for the single field case
    * probably others

    Can you raise a JIRA issue for that?

    Comment


    • #3
      Thanks for clarifying this.

      For our case it would be sufficient if the relateTo() method would allow to create another relationship of the same type between two entities (either having a flag or by using a new method).
      We won't use @RelatedTo because we have to create a relationship entity anyway. So for our usecase everything could be as it is, but having a method relatoTo() that allows us to create this relationship.

      I'll raise a JIRA issue for that.

      Comment


      • #4
        In the worst case you could drop down to the Neo4j level

        entity1.getPersistentState().createRelationshipTo( entity2.getPersistentState(),DynamicRelationshipTy pe.withName("REL_TYPE"));

        The relateTo method delegates to the current infrastructure which enforces the mentioned constraints. So I have to outfit the infrastructure with appropriate methods/flags to handle these cases.
        I'm not so sure how those additional relationships would then interfere with the classical @RelatedTo fields or if they would lead to random errors that are not explainable by the users. So I'd probably choose to add the flag on the field annotation.

        Michael

        Comment


        • #5
          I just wrote a function that creates the relationship as you proposed. :-)

          BTW: I opened an issue on JIRA for this.


          Best regards,
          James

          Comment


          • #6
            resolved in 2.0-SNAPSHOT

            Comment


            • #7
              Hello,

              I'm also trying to manage how to establish multiple relationships between two same nodes in a graph. In my case, it could be thought as a comment posted by a user into an concrete item.

              I'm trying to figure out how to do it in the "low level" way, but cannot get to. Once i get both start and end nodes (annotated types), i tried to do it with the RelationshipOperationsRepository injected implementation, but it also clears previous relationship instances (note: both have extra properties: ie, the comment itself and the posting date, and it even returns me different id's).

              Reading this thread i understand i could do it with the new relateTo method you mentioned, but how can I get to it, or to the PersistentState instance for my two nodes from my repository?

              Best regards, and keep doing this great work... really amazing stuff

              Jp

              Ps: i'm using spring-data-neo4j 2.0-RC1

              Comment


              • #8
                Ok, just autowiring neo4jtemplate i got the createrelationship with the allowduplicates param...

                Maybe it would be interesting to have it also in RelationshipOperationsRepository. What do you think?

                For those interested:

                Code:
                PropertyContainer originPC  = this.neo4jTemplate.getPersistentState( origin  );
                PropertyContainer targetPC = this.neo4jTemplate.getPersistentState( target );
                		
                Comments commentRegistry;
                						
                commentRegistry = this.neo4jTemplate.createRelationshipBetween(
                	originPC, targetPC, Comments.class, "comments", true
                );
                
                commentRegistry.setDate()
                commentRegistry.setComment()
                иии
                this.commentsRepository.save(commentRegistry);

                Thanks again... and congrats... again too


                Jp

                Comment


                • #9
                  ok, so you think the additional method would make sense in the RelationshipOperationsRepository ?

                  like
                  Code:
                     repo.createDuplicateRelationshipBetween(start,end, class, relType)
                  while thinking about it, a Relationship-repository should probably also provide those?
                  But without the need of providing the entity-class / type ?

                  Could you raise a JIRA issue for both?

                  Thanks a lot

                  Michael

                  Comment


                  • #10
                    Dont know if having a createDuplicateRelationship with no Entity class would make sense, i mean, in that case, there would be no way of distinguishing two relationships of the same type between the same two nodes (from the application point of view), but it does make sense when having properties in the relationships, as it would give more semantics to the relationship appart from it's type.

                    Thanks!

                    Jp

                    Comment


                    • #11
                      Oh, right you are. I was already thinking being inside of a RelationshipRepository

                      Comment

                      Working...
                      X