简体   繁体   English

是否可以在具有相同 label 的节点之间创建双向关系?

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

Person node:人物节点:

  • firstName
  • lastName
  • address地址
  • email email
  • phoneNumber电话号码

Company node:公司节点:

  • Name姓名
  • address地址
  • email email
  • phoneNumber电话号码

Relationships:关系:

  • Person -[SPOUSE]-> Person人-[配偶]-> 人
  • Person -[SIBLING]-> Person人 -[SIBLING]-> 人
  • Person -[FAMILY]-> Person人-[家庭]-> 人
  • Company -[EMPLOYEE]-> Person公司 -[EMPLOYEE]-> 个人

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.当我为 Jane 创建 Person 时,我还添加了与 John 的兄弟关系。

Running person.fetchById("29d31f6c-edfe-48a2-9ab2-3baed5d5ae69") retrieves the node Jane with the corresponding sibling node 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
}

However, the sibling relationship should be bi-directional.但是,兄弟关系应该是双向的。 Running person.fetchById("f825cedd-7328-4f9d-b0fd-a33726814f25") only retrieves the node John.运行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
}

Here is where the problem lies.这就是问题所在。 I could add another sibling relationship between John and Jane.我可以在 John 和 Jane 之间添加另一个兄弟关系。 But, this effectively creates a infinite loop between the two.但是,这有效地在两者之间创建了无限循环。 And the output from person.fetchById ends up being garbage.而来自person.fetchById的 output 最终成为垃圾。

  1. Is there any way to limit the depth of nodes returned when fetching nodes?有没有办法限制获取节点时返回的节点深度?
  2. I'm new to neo4j, so I suspect my design is wrong.我是 neo4j 的新手,所以我怀疑我的设计是错误的。 What is the best way to model this kind of relationship? model这种关系的最佳方法是什么?

You're using SDN here?你在这里使用SDN?

From the Spring Data Neo4j docs:来自 Spring 数据 Neo4j 文档:

If you don't care about the direction then you can specify direction=Relationship.UNDIRECTED which will guarantee that the path between two node entities is navigable from either side.如果您不关心方向,则可以指定direction=Relationship.UNDIRECTED,这将保证两个节点实体之间的路径可以从任一侧导航。

As far as I know (i'm just beginning to learn Neo4j too) you can't have bi-directional relationships, it's always suggested to have two directional ones.据我所知(我也刚刚开始学习 Neo4j)你不能有双向关系,总是建议有两个方向的关系。

If you do decide to add another relationship in the other direction, you could limit the number of hops to just one:如果您决定在另一个方向添加另一个关系,则可以将跃点数限制为一个:

How can Cypher Impose a Maximum Number of Hops Only Counting a Specific Type of Node? Cypher 如何仅针对特定类型的节点施加最大跳数?

Or if you want to see longer chains of relationships, perhaps put a condition on the results to not return the originating node in the results?或者,如果您想查看更长的关系链,也许在结果中设置一个条件,不返回结果中的原始节点?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM