gpt4 book ai didi

java - Spring 数据neo4j : correct way to handle relationships?

转载 作者:行者123 更新时间:2023-12-01 09:35:21 26 4
gpt4 key购买 nike

我有 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 和 #2 中的描述设置对象模型是有意义的。

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

关于java - Spring 数据neo4j : correct way to handle relationships?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39020458/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com