Person node:
- firstName
- lastName
- address
- phoneNumber
Company node:
- Name
- address
- phoneNumber
Relationships:
- Person -[SPOUSE]-> Person
- Person -[SIBLING]-> Person
- Person -[FAMILY]-> Person
- Company -[EMPLOYEE]-> Person
Person entity:
public class Person {
@Id
@GeneratedValue
Long personId;
@Builder
public Test(Long 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. 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
}
Here is where the problem lies. 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.
- Is there any way to limit the depth of nodes returned when fetching nodes?
- I'm new to neo4j, so I suspect my design is wrong. What is the best way to model this kind of relationship?