【问题标题】:Is it possible to create bi-directional relationships between nodes with the same label?是否可以在具有相同标签的节点之间创建双向关系?
【发布时间】: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 的输出最终变成了垃圾。

  1. 有什么方法可以限制获取节点时返回的节点深度吗?
  2. 我是neo4j 的新手,所以我怀疑我的设计是错误的。模拟这种关系的最佳方式是什么?

【问题讨论】:

    标签: java spring-boot neo4j spring-data-neo4j


    【解决方案1】:

    您在这里使用的是 SDN?

    来自 Spring Data Neo4j 文档:

    如果您不关心方向,则可以指定direction=Relationship.UNDIRECTED,这将保证两个节点实体之间的路径可从任一侧导航。

    【讨论】:

    • 这在 6.0.7 中可用吗?
    【解决方案2】:

    据我所知(我也刚开始学习 Neo4j)你不能有双向关系,总是建议有两个方向的关系。

    如果您决定在另一个方向添加另一个关系,则可以将跃点数限制为一个:

    How can Cypher Impose a Maximum Number of Hops Only Counting a Specific Type of Node?

    或者,如果您想查看更长的关系链,或许可以在结果中设置一个条件,不返回结果中的原始节点?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多