Neo4j中双向关系的命名约定(使用Spring数据) [英] Naming convention for bidirectional relationships in Neo4j (using Spring Data)

查看:302
本文介绍了Neo4j中双向关系的命名约定(使用Spring数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些建议来命名人际关系.另外,我不确定如何用spring数据注释域实体.我见过的大多数示例都是单向的,选择的名称非常简单.

I need some advice to give name to relationships. Plus, I'm not sure on how I have to annotate my domain entities with spring data. Most of examples that I've seen are unidirectional and the name chosen are pretty straightforward.

假设以下示例:

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

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

关系名称似乎还可以,没问题.

The relationship name seems ok, no problem.

现在假设我想使这种关系成为双向的.以下方法有什么区别(和优点/缺点).

Now suppose that I want to make this relationship bidirectional. What are the differences (and the pros/cons) of the following approaches.

1)将关系的两个方向都设为Direction.BOTH,然后将关系类型命名为"OWNERSHIP"?

1) Make both sides of the relationship direction=Direction.BOTH and call the relationship type="OWNERSHIP"?

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

    @RelatedTo(type="OWNERSHIP", direction=Direction.BOTH)
    private Set<Car> cars;
}

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

    @RelatedTo(type="OWNERSHIP", direction=Direction.BOTH)
    private Person person;
}

2)双方都使用方向吗?

2) Use direction on both sides?

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

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

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

    @RelatedTo(type="OWNED_BY", direction=Direction.INCOMING)
    private Person person;
}

推荐答案

Neo4j中没有双向关系.每个关系都有一个开始节点和一个结束节点.也就是说,您可以选择在编写遍历时忽略该方向,有效地将这种关系用作双向关系.

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.

您要建模的是拥有汽车的人. (人)-[:: OWNS]->(汽车)意味着两件事:从人的角度来看,这种外向关系表明该人拥有汽车.从汽车的角度来看,相同(但传入)的关系意味着它是人所有的.

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.

如果未指定方向,则默认情况下,SDN中的@RelatedTo注释将使用Direction.OUTGOING.这就是为什么您可能认为这些关系是双向的,但并非如此.默认情况下,它们正在传出.

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;
}

这篇关于Neo4j中双向关系的命名约定(使用Spring数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆