Search code examples
javaneo4jspring-data-neo4jneo4j-ogm

NodeEntity cant be cast to graphdb.Node


I have a NodeEntity like the following and I want to use Pathfinder algorithm on it. The algorithm is below. unfortuantely I get the error that neo4j.entity.Stop cannot be cast to org.neo4j.graphdb.Node

What should I do? I dont know how to make spring use the org.neo.graphalgo graphalgo library

private Path testGraphAlgoFactory(Stop s1, Stop s2){

 PathExpander expander = PathExpanders.allTypesAndDirections();

 PathFinder<Path> pathFinder =
                GraphAlgoFactory.shortestPath(expander , 6);

 Path path = pathFinder.findSinglePath((Node)s1, (Node)s2);
}

This is my NodeEntity class as follows:

@NodeEntity
public class Stop {
    @GraphId
    private Long id;

    @Property(name="name")
    private String name;

    @Property(name="lon")
    private double longitude;

    @Property(name="lat")
    private double latitude;

    @Property(name="id")
    private String stopId;
}

Solution

  • If you are using OGM to convert the nodes from the graph into objects this is a valid outcome. The classes like Stop are "just" Java classes and do not know that they have any Neo4j origin.

    Besides this (and also regarding you other question from earlier this day): the graph algorithms are intended to be used as a plugin in Neo4j and not as part of your application code. Neo4j Graph Algorithms documentation

    So it might be easier in your application to use the SessionFactory and get a new Session to run a cypher statement that calls the algorithm on your Neo4j. e.g. CALL algo.shortestPath(...)

    Another option could be that you use a custom query in your SpringData Neo4j repository to execute the query statement by using @Query("CALL ..."). But you must know what you will return from your cypher statement. Then you can put the result into a @QueryResult class. Sample from the docs (but do not create an inner class but a class in your entity scan path)