问题
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 @NodeEntity
s: 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?
回答1:
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.
来源:https://stackoverflow.com/questions/29613795/neo4j-relationship-between-three-nodes