无法迭代ExecutionResult

时间:2014-04-14 12:10:33

标签: java neo4j cypher resultset

我编写了一个测试,从here复制官方密码查询示例并添加了所需的强制转换。然后它失败了: java.lang.ClassCastException: scala.collection.Iterator$$anon$11 cannot be cast to java.util.Iterator ...。这是代码:

   
@Test
public void testCreateWithCypher() {
  ExecutionEngine engine = new ExecutionEngine(db, StringLogger.SYSTEM);
  try (Transaction transaction = db.beginTx()) {
    String cypher = "CREATE (x:Person {name: 'John'})" +
                    "RETURN x";
    ExecutionResult result = engine.execute(cypher);
    Iterator<Node> n_column = (Iterator<Node>) result.columnAs("x");
    for (Node node : IteratorUtil.asIterable(n_column)) {
      String nodeResult = node + ": " + node.getProperty("name");
    }
    transaction.success();
  }
}

没有强制转换,它将无法编译,因为columnAs返回Iterator<Object>。这个scala.collection.Iterator$$anon$11顺便做什么?我认为extended java.lang.Iterator

我只想要Iterator<Node>,以便我可以从ExecutionResult获取内容。没有演员怎么办呢?我的项目使用${neo4.version} = 2.0.1:

的这些依赖项    
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j</artifactId>
        <version>${neo4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-cypher</artifactId>
        <version>${neo4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-kernel</artifactId>
        <version>${neo4j.version}</version>
        <type>test-jar</type>
    </dependency>

1 个答案:

答案 0 :(得分:2)

您可能正在使用org.neo4j.cypher.ExecutionEngine而不是org.neo4j.cypher. javacompat .ExecutionEngine。第一个是在Scala中实现的,您无法直接将Scala对象转换为Java对象(AFAIK)。