0
votes

I am getting trouble with creating nodes and relationship. I can create two nodes with a relationship by using java code below:

URI Node1 = add.createNode();
URI Node2=add.createNode();
URI relation1 = add.addRelationship(Node1,Node2, "wasGeneratedBy", "{}");

add is created previously.

But if I have node2 already in the Neo4j Graph database, and I just want to create a new node1 and a new relationship then connect to the Node2 inside the Neo4j, how to make this happen?

1
I assume you are doing this by the REST API by your notations. As Jatin mentioned, you will need to get the reference to node2, then pass it into the addRelationshipNicholas

1 Answers

2
votes

You need to get reference to the other node. There are multiple ways of doing it. One way is that you know the path from the reference Node to it. Say:

graphDb = new EmbeddedGraphDatabase( DB_PATH );
Node node2 = graphDb.createNode();
graphDb.getReferenceNode().createRelationshipTo(
            node2, RelTypes.USERS_REFERENCE );

Later you can get access to node2 from the getReferenceNode.

Another way would be to index it. And then use Index to get reference to the Node. I would recommend looking at the documentation and the sample examples as they are extensive and very good.