4

I'm totally new to Neo4j and I'm testing it in these days. One issue I have with it is how to correctly implement a relationship which involves 3 different nodes using Spring Data. Suppose, for example, that I have 3 @NodeEntitys: User, Tag and TaggableObject.

As you can argue, a User can add a Tag to a TaggableObject; I model this operation with a @RelationshipEntity TaggingOperation. However, I can't find a simple way to glue the 3 entities inside the relationship. I mean, the obvious choice is to set @StartNode User tagger and @EndNode TaggedObject taggedObject; but how can I also add the Tag to the relationship?

tigerjack
  • 1,158
  • 3
  • 21
  • 39

1 Answers1

5

This is called a "hyperedge", I believe, and it's not something that Neo4j supports directly. You can create an additional node to support it, tough. So you could have a TagEvent node with a schema like so:

(:User)-[:PERFORMED]->(:TagEvent)
(:Tag)<-[:USED]-(:TagEvent)
(:TagObject)<-[:TAGGED]-(:TagEvent)

Another alternative is to store a foreign key as a property on a relationship or a node. Obviously that's not very graphy, but if you just need it for reference that might not be a bad solution. Just remember to not use the internal Neo4j ID as in future versions that may not be dependable. You should create your own ID for this purpose.

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • thanks for your help Brian. I think your `TagEvent` is pretty similar to my `TagOperation`. So, is there a way to specify your schema in `Spring Data Neo4j`, using annotations or so? Are your approach transactional? I mean, how can I assure that when a `User` performs a `TagEvent` using a certaint `Tag` the USED relationship is automatically created? – tigerjack Apr 13 '15 at 20:17
  • Sorry, I've not used Spring Data Neo4j before ;) I've mainly just worked with Cypher – Brian Underwood Apr 13 '15 at 20:59
  • Thanks for your help anyway. So, what do you think is the best way to implement it in Java code? – tigerjack Apr 15 '15 at 13:12
  • I'll let somebody else comment. I haven't written much Java in a long time ;) – Brian Underwood Apr 15 '15 at 13:13