无法使用Java中的Cypher查询在Neo4J数据库中创建两个节点之间的关系

时间:2014-01-29 12:49:21

标签: java neo4j cypher

我创建了具有一些属性的节点,现在我想在创建它们之后创建一个关系。

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);

我收到以下错误,声明在创建节点之后无法创建节点之间的关系。

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)

1 个答案:

答案 0 :(得分:3)

createRelationshipTo与节点a和b

一起使用

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

如果使用cypher

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

这将创建 - [:LIKES] - &gt; b