@Relationship Sets vs single Object reference in Time-based Graphs

(Usuallycwdillon) #1

I have a conceptual issue that I'm not sure how to solve. What if my data graph is ontologically different than my business logic?

I have a time-based graph with Countries and Territories, where a Country can occupy only one Territory at any point in time. In my data, I have time-boxed relations, for example:

(:Territory{name:Germany of 1918})<-[:OCCUPIES{from:1918, until:1944}]-(c:Country{name:Germany})-[:OCCUPIES{from:1945, until:1993})->(:Territory{name:Germany of 1945})

The OGM is expecting my Country class to have a @Relationship Set<Territory> territories; because of the data (and it wouldn't know which relationship I want to use for any given time, anyway).

I realize that I could set another @Relationship Territory currentTerritory attribute, but I can only use the :OCCUPIES relationship name on one or the other, right?

(Gerrit Meier) #2

You are right. This constellation is not resolvable for Neo4j-OGM (as described here).

One solution could be to introduce a subclass of Territory like CurrentTerritory that would be the more concrete implementation and get this additional label. As a consequence the relationship type is not ambiguous anymore.

(Usuallycwdillon) #3

Thanks @gerrit.meier,
I think I understand how the relationships work a bit better. But, now I'm really confused about inheritance and how labels work with inheritance.

The documentation on inheritance in the OGM manual is a little sparse and my Googling suggests that this is a bit of a moving target right now. Is there an example of this somewhere (that doesn't assume I'm using Spring and can define Repositories)?

(Gerrit Meier) #4

There is a short explanation in the docs (under the code snippet). This is the one you might refer to.
In general any (abstract) class annotated with @NodeEnity in the class hierarchy adds it label to the resulting instance/node. In your case this would be (:Territory:CurrentTerritory) assuming that Territory is your top-most class.
If you would change the type of currentTerritory to CurrentTerritory instead of "just" Territory, Neo4j-OGM can distinguish between them both because it takes the bottom-most label matching class from your hierarchy that fits with the labels returned from the database for this node.