user1002065
user1002065

Reputation: 615

Spring data for neo4j generating dynamic relationships between entities

I just started reading and playing with graph db and specifically Neo4j.

However I didn't encounter a solution for a use case that seems to me very common. Assume I have User object in my system and I want to maintain the relationships between users. Examples:

  1. User1 -> User2 - relationship sibling
  2. UserX -> UserY - relationship parent
  3. UserY -> UserX - relationship child
  4. UserX -> UserZ - relationship teacher

I would like to store the relationship (the edge between the nodes) dynamically and not creating an entity with all possible relationships entities with annotation @Relationship.

Later, I would like to get a user and all his connections, but in run-time to figure out what is the type of the relationship.

Is this possible with spring data? or maybe it is not possible at all using Neo4j?

I will appreciate if you can point me to some reading for a solution to my problem.

Thank you.

Upvotes: 0

Views: 783

Answers (1)

meistermeier
meistermeier

Reputation: 8262

It seems that you are only interested in the type of relationship after you would query for all, right?

You could use a @RelationshipEntity and add a property to it to define its type.

@RelationshipEntity(type = "USER_RELATION")
public class UserRelation {
   //... @StartNode/@EndNode/@Id
   private String type; // Here goes sibling, parent, etc.
}

And in your User entity you would just define one relationship.

@Entity
public class User {
  // ... id, etc.
  @Relationship(type = "USER_RELATION")
  private List<UserRelation> userRelations;
}

This would always query every outgoing connection to another user.

Upvotes: 0

Related Questions