Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 1: replicate users.

How to Model complex identities in OGM / Graph edges - including date ranges

In our application we manage some different kinds of relationships like
"factory" --produces--> "product"

We modeled this with OGM by creating the node entities and a relation entity. We created only one Neo4jRepository for the realation.

My question is how to model and cope with complex identity. On first sight 2 relations are identical if start and end node are the same but in our case we added validity dates to the relation.

Each relation has a validFrom and validTo date.

The question is how and where to check if there is already a relation for a given date range or with an overlapping date range:

  • is there a elegant and efficient way to model this with OGM or Cypher and let Neo4J check for interfering identities?

  • or should we do this checks in the service layer (spring boot). in this case we have to query always all relations before we save a new one, right?

Thanks in advance



It is possible with Spring Data Neo4j (I assume that you are stil referring to the same project as in the other question) but it looks at least a little bit hacky.

List<Produces> findByProductionDateFromBeforeAndProductionDateToAfter(LocalDate inRange1, LocalDate inRange2);

with inRange1=inRange2 returns something like

[Produces{(factory=f1)->(product=p1) productionDateFrom=2018-04-15 / productionDateTo=2019-04-14}, Produces{(factory=f1)->(product=p1) productionDateFrom=2017-04-15 / productionDateTo=2018-04-14}]
[Produces{(factory=f1)->(product=p1) productionDateFrom=2018-04-15 / productionDateTo=2019-04-14}]

for my test case where

LocalDate dateInP2 = LocalDate.of(2018, Month.MAY, 1);
System.out.println(repository.findByProductionDateFromBeforeAndProductionDateToAfter(dateInP2, dateInP2));

I do think that a custom @Query might be nicer because there would be just one parameter in the method signature.


yeas it is the same project :-).
I will first try the the Spring Data way (thanks for this hint), probably i will create a wrapping method that takes the relation and extracts both dates from it.

But sooner or later we will get in touch with Cypher and then we will refactor it.

At the moment we are still exploring different data models and als Neo4J itself.