simplyblue
simplyblue

Reputation: 2339

Unable to create relationship between two nodes in Neo4J database using Cypher query in Java

I have created nodes with some properties, now i want to create a relationship after creating them.

ExecutionEngine conn = new ExecutionEngine(db);

            ExecutionResult result;
            ExecutionResult result1;
            ExecutionResult result2;

            Node a = null,b = null;
result = conn.execute("start n=node(*) where has(n.name) and n.name='Manoj' return n"); 

            Iterator<Node> n_column = result.columnAs("n");


            for(Node node: IteratorUtil.asIterable(n_column))
            {
                a = node;
                break;
            }

result1 = conn.execute("start n=node(*) where has(n.name) and n.name='Bobby' return n");    

            Iterator<Node> n_column1 = result1.columnAs("n");


            for(Node node: IteratorUtil.asIterable(n_column1))
            {
                b = node;
                break;
            }
result2 = conn.execute("create (" + a + ")-[:likes]->(" + b + ")");

            System.out.println(result2);

I get the following error, stating that it cant create relationship between nodes after creating them.

Exception in thread "main" Node `Node` has already been created. Can't assign properties to it again.
    at org.neo4j.cypher.internal.executionplan.builders.UpdateCommandExpander$$anonfun$distinctify$1$1.apply(CreateNodesAndRelationshipsBuilder.scala:79)
    at org.neo4j.cypher.internal.executionplan.builders.UpdateCommandExpander$$anonfun$distinctify$1$1.apply(CreateNodesAndRelationshipsBuilder.scala:76)
    at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:200)
    at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:200)
    at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
    at scala.collection.immutable.List.foreach(List.scala:45)
    at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:200)
    at scala.collection.immutable.List.flatMap(List.scala:45)
    at org.neo4j.cypher.internal.executionplan.builders.UpdateCommandExpander$class.distinctify$1(CreateNodesAndRelationshipsBuilder.scala:76)
    at org.neo4j.cypher.internal.executionplan.builders.UpdateCommandExpander$class.expandCommands(CreateNodesAndRelationshipsBuilder.scala:121)
    at org.neo4j.cypher.internal.executionplan.builders.CreateNodesAndRelationshipsBuilder.expandCommands(CreateNodesAndRelationshipsBuilder.scala:34)
    at org.neo4j.cypher.internal.executionplan.builders.CreateNodesAndRelationshipsBuilder.apply(CreateNodesAndRelationshipsBuilder.scala:39)
    at org.neo4j.cypher.internal.executionplan.ExecutionPlanImpl.prepareExecutionPlan(ExecutionPlanImpl.scala:45)
    at org.neo4j.cypher.internal.executionplan.ExecutionPlanImpl.<init>(ExecutionPlanImpl.scala:31)
    at org.neo4j.cypher.ExecutionEngine$$anonfun$prepare$1.apply(ExecutionEngine.scala:67)
    at org.neo4j.cypher.ExecutionEngine$$anonfun$prepare$1.apply(ExecutionEngine.scala:67)
    at org.neo4j.cypher.internal.LRUCache.getOrElseUpdate(LRUCache.scala:37)
    at org.neo4j.cypher.ExecutionEngine.prepare(ExecutionEngine.scala:67)
    at org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:59)
    at org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:54)
    at org.neo4j.cypher.javacompat.ExecutionEngine.execute(ExecutionEngine.java:66)
    at com.Neo4j.Login.test(Login.java:93)
    at com.Neo4j.Login.main(Login.java:34)

Upvotes: 0

Views: 785

Answers (1)

Sumeet Sharma
Sumeet Sharma

Reputation: 2583

Use createRelationshipTo with the nodes a and b

As in

Node a; //Some node
Node b; //Some node
a.createRelationshipTo(b,DynamicRelationshipType.withName("LIKES"));

In case of using cypher

String aId= a.getId();
String bId =b.getId();
engine.execute("start a=node("+aId+") , b=node("+bId+") create a-[:LIKES]->b ");

This will create a-[:LIKES]->b

Upvotes: 3

Related Questions