This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

AnnouncementAnnouncement Module

Collapse

No announcement yet.

Spring Data Graph: Why only one relationship of a type between two nodes?Page Title Module

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

Sep 14th, 2011, 09:03 AM

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

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?

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

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.

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.

Comment

Comment

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?

Comment

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.