cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Is it possible to create bi-directional relationships between nodes with the same label?

Hello,

Im new to Neo4j and was wondering if I could get some help. To try a learn how to use Neo4j I've created a simple address book application. Im using Java and Sprint Data Neo4j 6.0.7. I'm having a problem retrieving data where the node links to another node the with the same label, please see below:

Person node:

  • firstName
  • lastName
  • address
  • email
  • phoneNumber

Company node:

  • Name
  • address
  • email
  • phoneNumber

Relationships:

  • Person -[SPOUSE]-> Person
  • Person -[SIBLING]-> Person
  • Person -[FAMILY]-> Person
  • Company -[EMPLOYEE]-> Person

Person entity:

public class Person {
    @Id
    @GeneratedValue(UUIDStringGenerator.class)
    String personId;

    @Builder
    public Test(String personId, String firstName, String lastName, String address, String email, String phoneNumber) {
        this.personId = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.addresss = address;
        this.email = email;
        this.phoneNumber = phoneNumber
    }

    @NotEmpty(message = "Please provide a first name")
    String firstName;

    @NotEmpty(message = "Please provide a last name")
    String lastName;

    String address;

    String email;

    String phoneNumber;

    @Relationship(type = "SPOUSE",direction=Relationship.OUTGOING)
    public Set<Person> spouse;

    @Relationship(type = "SIBLING",direction=Relationship.OUTGOING)
    public Set<Person> sibling;

    @Relationship(type = "FAMILY",direction=Relationship.OUTGOING)
    public Set<Person> family;
}

When I create a Person for Jane I also add a sibling relationship with John.

Running person.fetchById("29d31f6c-edfe-48a2-9ab2-3baed5d5ae69") retrieves the node Jane with the corresponding sibling node John.

{
   "address":"",
   "email":"",
   "phoneNumber":"",
   "personId":"29d31f6c-edfe-48a2-9ab2-3baed5d5ae69",
   "firstName":"Jane",
   "lastName":"Smith",
   "spouse":null,
   "sibling":[
      {
         "address":"",
         "email":"",
         "phoneNumber":"",
         "personId":"f825cedd-7328-4f9d-b0fd-a33726814f25",
         "firstName":"John",
         "lastName":"smith",
         "spouse":null,
         "sibling":[],
         "family":null
      }
   ],
   "family":null
}

However, the sibling relationship should be bi-directional. However ,running person.fetchById("f825cedd-7328-4f9d-b0fd-a33726814f25") only retrieves the node John.

{
   "address":"",
   "email":"",
   "phoneNumber":"",
   "personId":"f825cedd-7328-4f9d-b0fd-a33726814f25",
   "firstName":"John",
   "lastName":"Smith",
   "spouse":null,
   "sibling":null,
   "family":null,
   "closeFriend":null,
   "friend":null
}

I could add another sibling relationship between John and Jane. But, this effectively creates a infinite loop between the two. And the output from person.fetchById ends up being garbage.

  1. Is there any way to limit the depth of nodes returned when fetching nodes with SDN?
  2. I'm new to neo4j, so I suspect my design is wrong. What is the best way to model this kind of relationship?
0 REPLIES 0