简体   繁体   English

Neo4J中无根据的关系

[英]Undirected Relationship in Neo4J

I am using Spring Data Neo4J to define a undirected relationship between different persons. 我使用Spring Data Neo4J来定义不同人之间的无向关系。 A sample entity class has been provided below. 下面提供了一个示例实体类。

@NodeEntity(label="Person")
public class Person {

    @GraphId
    private Long id;
    private String name;

    @Relationship(type = "FRIEND_WITH", direction=Relationship.UNDIRECTED)
    List<Person> friends;
}

As above, a Person has a list of friends of type "Person". 如上所述,Person具有“Person”类型的朋友列表。 The relationship is kept undirected to ensure that if a person A is "Friend_With" person B, then person B is also "Friend_With" Person A. 这种关系保持不变,以确保如果一个人A是“Friend_With”人B,那么B人也是“Friend_With”人A.

The code to add a friend is provided below 下面提供了添加朋友的代码

    if((person.getFriends() == null)||(person.getFriends().size()==0)){
        List<Person> friendList = new ArrayList<Person>();
        friendList.add(friend);
        person.setFriends(friendList);
    }else{
        person.getFriends().add(friend);
    }

    personRepository.save(person);

I have added PersonB as a friend of PersonA,so ideally, this should mean 我已将PersonB添加为PersonA的朋友,所以理想情况下,这应该意味着

PersonA - [:FRIEND_WITH] -> PersonB
PersonB - [:FRIEND_WITH] -> PersonA

as the relationship is undirected 因为这种关系是无向的

But when I am querying, in Neo4J with 但是当我在Neo4J中查询时

MATCH (p:Person)-[r:FRIEND_WITH]->(b:Person) where p.name = "PersonA" return p,b

I am getting the result as PersonA, PersonB. 我得到的结果是PersonA,PersonB。 But when I am querying 但是当我在询问时

MATCH (p:Person)-[r:FRIEND_WITH]->(b:Person) where p.name = "PersonB" 

no rows are returned. 没有返回任何行。 Thus the direction specified in the entity class does not seem to work. 因此,实体类中指定的方向似乎不起作用。 Also the Graph in Neo4J browser shows a directed edge from PersonA to PersonB. 此外,Neo4J浏览器中的Graph显示了从PersonA到PersonB的有向边。

All I want is that if PersonA is a friend of PersonB, the I will get the results, no matter, which way I ask. 我想要的是,如果PersonA是PersonB的朋友,我会得到结果,无论我问哪个方式。 The code seems to work for 代码似乎适用

MATCH (p:Person)-[r:FRIEND_WITH]-(b:Person) where p.name = "PersonB" 

where the "->" is replaced by "-", but I do not want to use this. 其中“ - >”替换为“ - ”,但我不想使用它。

What should I do ? 我该怎么办 ?

I am using spring-data-neo4j.version 4.0.0.RELEASE and spring-boot version spring-boot-starter-parent 1.3.0.M5 我使用的是spring-data-neo4j.version 4.0.0.RELEASE和spring-boot version spring-boot-starter-parent 1.3.0.M5

In Neo4j, ALL relationships are directed. 在Neo4j中,所有关系都是定向的。

However, you can have the notion of undirected edges at query time. 但是,您可以在查询时获得无向边的概念。 Just remove the direction from your MATCH query : 只需从MATCH查询中删除方向:

MATCH (p:Person)-[r:FRIEND_WITH]-(b:Person) where p.name = "PersonB" 

Why don't you want to use this ? 你为什么不想用这个?

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

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