Naming convention for bidirectional relationships in Neo4j (using Spring Data)

感情迁移 提交于 2019-12-06 15:58:37

There is no such thing as bidirectional relationships in Neo4j. Each relationship has a start node and an end node. That said, you can choose to ignore that direction when writing traversals, effectively using the relationship as a bidirectional one.

What you're trying to model is people owning cars. (person) -[:OWNS]-> (car) means two things: from the person's point of view, this outgoing relationship shows the person owns the car. From the car's point of view, the very same (but incoming) relationship means it is owned by the person.

The @RelatedTo annotation in SDN uses Direction.OUTGOING by default, if no direction is specified. This is why you might have thought these relationships are bidirectional, but they are not; they are OUTGOING by default.

So I would model your domain like this:

@NodeEntity
public class Person {   
   @Indexed
   private String name;

   @RelatedTo(type="OWNS", direction=Direction.OUTGOING) //or no direction, same thing
   private Set<Car> cars;
}

@NodeEntity
public class Car {   
   @Indexed
   private String description;

   @RelatedTo(type="OWNS", direction=Direction.INCOMING)
   private Person person;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!