Spring data neo4j:处理关系的正确方法?

Spring data neo4j: correct way to handle relationships?

我有 User 实体;用户可以是多个组的成员,也可以是一个组织的成员。有几个选项可以处理这种关系:

  1. Class User 有字段 Set<Group> groupsOrganization organization
  2. Classes GroupOrganization 有字段 Set<User> users
  3. 两个选项同时使用(某种双向关系)

此外,还有指定方向关系的注解:

Spring Data Neo4j ensures by default that there is only one relationship of a given type between any two given entities. The exception to this rule is when a relationship is specified as either OUTGOING or INCOMING between two entities of the same type. In this case, it is possible to have two relationships of the given type between the two entities, one relationship in either direction.

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.

来源:Good Relationships: The Spring Data Neo4j Guide Book

一旦我希望能够尽快获得组内的用户组和用户组,我就完成了一种同时使用上面列出的两个选项并注释每个关系的方法as UNDIRECTED 因为它看起来像通用方法。它有什么缺点吗?如果是这样,哪种方法会更好?

由于您要检索用户的组以及组中的用户,因此按照您在#1 和#2 中的描述设置对象模型是有意义的。

UNDIRECTED 在这里不是一个好的选择,因为它意味着用户和组之间的关系可以是任何方向的,我猜你不希望在你的图形模型中出现这种情况。 这适用于您不关心方向的关系(例如 (user1)-[:FRIEND]-(user2)),但不关心方向。 我会在 class 中使用 OUTGOINGINCOMING,具体取决于用户和组之间的实际关系。