OGM - function to add relationship without loading the relation objects in memory

So I like the ogm tool to insert data into the neo4j graph database, it feels natural to me, and worked for the first two small examples I tried, on the third example i used a bigger database and ran into out of memory in neo4j. i increased the memory from 1GB to 3GB for now, but i think there is an underlying problem with the way session.save(node) works when it comes to adding relationships unless there is a function I didn't see.

i can create a node and save it using session.save(node), then probably use cypher query in ogm to add the relationship based on the id of the nodes, but I think that would be a bit hacky and not in spirit of the ORM/OGM ways?

to add a relationship without using a cipher query,
I am making an object or node(node1) by querying a sql database then have to add every object connected to node1 then add it using the relationship function which adds the objects into a hashset
(node1) <- [(node2),(node3),...(noden)]

but i have to add all the relationship nodes otherwise I would be deleting previous relationship, i presume? which means there is a limit to the number of relationships that I can add because I have to keep all the objects related to a node in memory in the application and hope neo4j has enough memory to be able to handle that insertion/merge.

is there a way to say, node1.addthisrelation(node2.id) and save that into the database without loading or adding the entire node1 with all the associated nodes into memory? or addRelation(node1,node2,relationtype) or do I have to use cipher query and if so what the way to save node1 with id 1, has relationship x, to node2 with id 2?

Hey Amr,
I confess that I am not familiar with this ogm tool, but it strikes me that you are facing a similar problem that I had from a Spring Data perspective.
Whilst waiting for an informed answer, you may want to have a look at my humble effort at a cypher query at Adding to a list with Spring Data Neo4j with Kotlin in case you need to go in that direction.

Either way - good luck with your efforts.

1 Like

from the link (i removed the escapes)
MATCH (d:Diary)
WHERE id(d)=diaryId
WITH d
MATCH (e:Entry)
WHERE id(e)=entryId
CREATE (d)-[:DIARY_TO_ENTRY]->(e)

why the "with d" part? if you don't mind me asking.

I am not that familiar with cipher but i thought it would be label {id:diaryid}?
i thought the id would be in curly braces like (d:Diary{id:diaryId}) similar to the match queries i see,

match (d:Diary{name:'mydiary1'}) return d or match path = (:Diary{name:'mydiary1'}) return path

Hey Amr,

firstly, I should point out that I am not that familiar with Cypher myself.
I do not have any formal certification and the code was very much crafted on a suck it and see basis.
Personally, I would have liked to put the WHERE details in the MATCH parentheses, along the lines that you have suggested - I think it would have looked cleaner.
I tried a few alternatives, but I could not get that to work though, so I suspect that ids are treated differently to other attributes.

As for the WITH, I don't mind you asking at all :slightly_smiling_face:
Unfortunately I cannot give you a satisfactory answer, or at least one that I would have confidence in.
It feels as though it is necessary to pass a parameter into the next MATCH clause.
I will leave it to another member of the community to contribute a more informed view though.

1 Like

I tried this in neo4j sandbox, and it seems to work

Merge (p:Product {name: 'p1'})-[:Includes]-(c:Component {name: 'water'})
RETURN p,c

or Merge (:Product {name: 'p1'})-[:Includes]-(:Component {name: 'water'})

when I tried where it said with is required, this worked,

merge (p:Product{name:'p1'})
merge (c:Component{name:'water'})
with p,c
match (p:Product),(c:Component)
where p.name = 'p1' and c.name = 'water'
Merge (p)-[:Includes]-(c)
RETURN p,c

but it seems too cumbersome compared to Merge (:Product {name: 'p1'})-[:Includes]-(:Component {name: 'water'})

1 Like

Hey Amr,

I have not played with MERGE, as of yet, but I might look into it over the weekend.
Thanks for bringing it to my attention.

:+1:

1 Like