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:
- Class
User
has fieldsSet<Group> groups
andOrganization organization
- Classes
Group
andOrganization
has fieldsSet<User> users
- 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?