0
votes

I have the User entity; the user can be a member of multiple groups and be a member of one organization. There are several options to handle such relationships:

  1. Class User has fields Set<Group> groups and Organization organization
  2. Classes Group and Organization has fields Set<User> users
  3. Both options are used simultaneously (kind of bidirectional relationship)

In addition, there are annotations for relationships with specifying directions:

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.

Source: Good Relationships: The Spring Data Neo4j Guide Book

As soon as I want to be able to get the groups of user and users within the group as fast as I can, I finished with an approach using two options listed above at the same time as well as annotating every relationship as UNDIRECTED because it looks like the universal approach. Does it have any drawbacks? If so, which approach would be better?

1

1 Answers

1
votes

Since you want to retrieve groups for a user, and users in a group, it makes sense to set up your object model as you describe in #1 and #2.

UNDIRECTED is not a good choice here because it implies that the relationship between user and group can be in any direction, and I'm guessing you don't want this in your graph model. It's good for relationships where you don't care about the direction (such as (user1)-[:FRIEND]-(user2)) but not otherwise. I'd use OUTGOING and INCOMING in either class depending on what your relationship between user and group actually is.