【发布时间】:2021-03-31 14:50:29
【问题描述】:
人物节点:
- 名字
- 姓氏
- 地址
- 电子邮件
- 电话号码
公司节点:
- 姓名
- 地址
- 电子邮件
- 电话号码
关系:
- 人 -[配偶]-> 人
- 人 -[SIBLING]-> 人
- 人员 -[FAMILY]-> 人员
- 公司 -[EMPLOYEE]-> 人
个人实体:
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;
}
当我为 Jane 创建 Person 时,我还添加了与 John 的兄弟关系。
运行 person.fetchById("29d31f6c-edfe-48a2-9ab2-3baed5d5ae69") 检索节点 Jane 和对应的兄弟节点 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
}
但是,兄弟关系应该是双向的。运行 person.fetchById("f825cedd-7328-4f9d-b0fd-a33726814f25") 只会检索节点 John。
{
"address":"",
"email":"",
"phoneNumber":"",
"personId":"f825cedd-7328-4f9d-b0fd-a33726814f25",
"firstName":"John",
"lastName":"Smith",
"spouse":null,
"sibling":null,
"family":null,
"closeFriend":null,
"friend":null
}
这就是问题所在。我可以在 John 和 Jane 之间添加另一个兄弟关系。但是,这有效地在两者之间创建了无限循环。 person.fetchById 的输出最终变成了垃圾。
- 有什么方法可以限制获取节点时返回的节点深度吗?
- 我是neo4j 的新手,所以我怀疑我的设计是错误的。模拟这种关系的最佳方式是什么?
【问题讨论】:
标签: java spring-boot neo4j spring-data-neo4j