我有一个如下所示的 NodeEntity,我想对其使用 Pathfinder 算法。算法如下。不幸的是,我收到 neo4j.entity.Stop 无法转换为 org.neo4j.graphdb.Node 的错误
我该怎么办?我不知道如何让spring使用org.neo.graphalgo graphalgo库
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);
}
这是我的 NodeEntity 类,如下所示:
@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;
}
最佳答案
如果您使用 OGM 将图中的节点转换为对象,这是一个有效的结果。
像 Stop
这样的类“只是”Java 类,并不知道它们有任何 Neo4j 起源。
除此之外(以及今天早些时候关于您的其他问题):图形算法旨在用作 Neo4j 中的插件,而不是作为应用程序代码的一部分。 Neo4j Graph Algorithms documentation
因此,在您的应用程序中使用 SessionFactory 并获取新的 Session 来运行调用 Neo4j 上算法的 cypher 语句可能会更容易。例如调用 algo.shortestPath(...)
另一个选择可能是您在 SpringData Neo4j 存储库中使用自定义查询,通过使用 @Query("CALL ...")
来执行查询语句。但您必须知道将从密码语句中返回什么。然后您可以将结果放入 @QueryResult
类中。 Sample from the docs (但不要创建内部类,而是在实体扫描路径中创建一个类)
关于java - NodeEntity 无法转换为 graphdb.Node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48509276/