Announcement Announcement Module
No announcement yet.
Neo4J SpringData relationship mapping. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Neo4J SpringData relationship mapping.

    It is not clear to me the meaning (and the effect) of specifying BOTH type relationships.
    If I have a User class with a 1:1 relationship with a UserProfile class, should i specify:

    @RelatedTo(type = "USER_PROFILE", direction = Direction.BOTH, elementClass = UserProfile.class)
    protected UserProfile profile;

    and, in the UserProfile class, a
    @RelatedTo(type = "USER_PROFILE", direction = Direction.BOTH, elementClass = User.class)
    private User user;

    or could be better to have
    @RelatedTo(type = "USER_PROFILE", direction = Direction.OUTGOING, elementClass = UserProfile.class)
    protected UserProfile profile;

    and, in the UserProfile class, a
    @RelatedTo(type = "USER_PROFILE", direction = Direction.INCOMING, elementClass = User.class)
    private User user;

    1) In the Neo4J Graph DB, would there be a couple of directed edges between one user instance and one profile instance or a single edge?
    2) What is the main difference between the two solutions?

    Actually, it is not clear to me the meaning of this text from Hunger's GOOD RELATIONSHIPS book:

    "By setting direction to BOTH, relationships are created in the outgoing direction, but when the 1:N field
    is read, it will include relationships in both directions. A cardinality of M:N is not necessary because
    relationships can be navigated in both directions."

  • #2

    When you declare the relationship direction to be BOTH, then when you read an entity it will fetch both incoming and outgoing relationships with matching end nodes.

    It is the writing part that is interesting: we do not create two relationships in opposite directions; rather, the first outgoing relationship that is encountered is created, and it really doesn't matter which direction that is - see above.

    If you care about the direction, specify OUTGOING and INCOMING directly. OUTGOING is the default, so don't bother. And Incoming really just means: "read incoming relationships with matching end nodes". INCOMING is never traversed for creation, update or delete.

    And of course, if you want relationships in both directions (I don't think that would ever matter, other than for completeness - in Cypher for example you can draw all combinations of in, out and both) you specify OUTGOING for both, but then you must remember to save both ends properly - at that point they are independent.

    That's a lot of text: I suggest writing a test to convince yourself




    • #3
      Thank you. It is a little bit more clear now!