Kit Ostrihon
Kit Ostrihon

Reputation: 834

Create relationship between a new neo4j Node and an already existing one in Java

I am using Java API for Neo4j embedded db.

While saving an instance as a Node and I would like to create a Relationship from it to another node, that I have no reference other than a property (id/key).

As I understand it, if I'd have the two nodes I would just use:

nodeBeingSavedSeparately.createRelationshipTo(
            nodeToHaveRelationshipTo,
            RELATIONSHIP_TYPE
);

But I am just adding a new node and I would like to have the relationship with another already existing node, not to create a new node.

Is it possible to get the right instance of Node from the database and use it in that method? Something like:

nodeBeingSavedSeparately.createRelationshipTo(
            getNodeByProperty("idPropertyOfTheNodeToHaveRelationshipTo"), 
            RELATIONSHIP_TYPE
);

I found out, that there is a getNodeById(long); method, but there is no Node.setId(long); method. How do I set/get the right reference for the relationship?

Upvotes: 0

Views: 662

Answers (1)

cybersam
cybersam

Reputation: 66999

You can use either findNode or findNodes in GraphDataBaseService to get the existing node.

Upvotes: 1

Related Questions