質問

I have written a test, copying the official cypher query example from here and adding a needed cast. Then it fails with: java.lang.ClassCastException: scala.collection.Iterator$$anon$11 cannot be cast to java.util.Iterator .... This is the code:

@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();
  }
}

Without the cast, it will not compile, since columnAs returns Iterator<Object>. What is this scala.collection.Iterator$$anon$11 doing the by the way? I thought it extended java.lang.Iterator?

I just want an Iterator<Node> so that I can get stuff from the ExecutionResult. How can do that without the cast? My project uses these dependencies for ${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>
役に立ちましたか?

解決

You are probably using org.neo4j.cypher.ExecutionEngine instead of org.neo4j.cypher.javacompat.ExecutionEngine. The first one is implemented in Scala and you can't directly cast Scala objects to Java ones (AFAIK).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top