Search code examples
neo4jneo4j-ogm

How does Session#save(...) identify changed entities?


The Neo4j OGM manual says:

Under the bonnet, the implementation of Session has access to the MappingContext that keeps track of the data that has been loaded from Neo4j during the lifetime of the session. Upon invocation of save() with an entity, it checks the given object graph for changes compared with the data that was loaded from the database. The differences are used to construct a Cypher query that persists the deltas to Neo4j before repopulating it’s [sic] state based on the response from the database server.

How does this actually work? Does it depend on entity classes overriding hashCode(), or what?


Solution

  • No, the OGM does not rely on the implementation of hashCode. Instead, it uses it's own hash based upon property values to detect if the node has changed. For relationships, the MappingContext holds sets of known relationships from entities loaded within the session, and upon save, that collection is reconciled with the relationships collected from the entities being saved. The delta, in terms of relationships added, modified, or deleted, are used to construct the Cypher query that is sent to Neo4j.