Issue
I am writing a java project to insert data in neo4j using a cypher query. I want to stop neo4j from creating a duplicate node, instead linking the other node with the existing node.
CREATE (n1:node {name:'Adam'})-[:born_in]->(n2:node {name:'USA'})
//and again
CREATE (n1:node {name:'Adam'})-[:worked_at]->(n2:node {name:'Apple'})
I want to create one node of Adam and two other nodes i.e. USA
and Apple
. Are there any checks in java to avoid duplications?
Solution
best way to create relationship between two nodes is
first get the two nodes.if they doesn't exists then create. Once the nodes are loaded, then simply create a relationship between them
in your case, create your nodes with MERGE
and then relationship
MERGE (n1:node {name:'Adam'})
MERGE (n2:node {name:'USA'})
MERGE (n1)-[r:born_in]->(n2)
this link will help you in understanding MERGE
https://neo4j.com/docs/cypher-manual/current/clauses/merge/
Answered By - Govind Singh
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)