Issue of casting Node neo4j

a 夏天 提交于 2019-12-25 00:32:23

问题


My code is below, As per the documentation it should have given me the node values but it is throwing me exception

Exception in thread "main" java.lang.ClassCastException: scala.collection.convert.Wrappers$SeqWrapper cannot be cast to org.neo4j.graphdb.Node
	at com.neo4j.performance.FetchData.main(FetchData.java:32)
I'm using Neo4j 2.2.2.
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.Transaction;
import java.util.Iterator; 
import org.neo4j.helpers.collection.IteratorUtil;




import com.neo4j.enitites.Global;

public class FetchData {
     public static void main(String[] args) throws Exception  
        {

         String nodeResult = null;
         try ( Transaction ignored = Global.db.beginTx();
                  Result result = Global.db.execute( "MATCH path=()-[:route_1*]-() RETURN nodes(path) AS result" ) )
            {
                // START SNIPPET: items
                Iterator<Node> n_column = result.columnAs( "result" );
                for ( Node node : IteratorUtil.asIterable( n_column ) )
                {
                    nodeResult = node + ": " + node.getProperty( "StationCode" );
                }
                // END SNIPPET: items


            }

         System.out.println(nodeResult);
        }
}

I've searched for the error but only 2 or 3 posts are there those also not working. Has anyone came across with this issue or there is something wrong in this code. Thanks


回答1:


Use this snippet instead inside the transaction try-with-resources block:

        for (Object cell : IteratorUtil.asIterable(result.columnAs("result"))) {
            Iterable<Node> nodes = (Iterable<Node>) cell;
            for (Node n : nodes) {
                System.out.println(n);
            }
        }


来源:https://stackoverflow.com/questions/31554217/issue-of-casting-node-neo4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!