[英]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)
Use createRelationshipTo
with the nodes a and b 对节点a和b使用createRelationshipTo
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 这将创建一个-[:LIKES]-> b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.