有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringDataNeo4j:处理关系的正确方法?

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

  1. User具有字段Set<Group> groupsOrganization organization
  2. 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

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


共 (1) 个答案

  1. # 1 楼答案

    因为您希望为用户检索组,以及组中的用户,所以按照#1和#2中的描述设置对象模型是有意义的

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