spring数据neo4j实体路径Map

2o7dmzc5  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(538)

我有一个独立的neo4j db2 .1.6。我从一个基于web的springboot项目开始,并添加了springdataneo4j3.2.1版本。我正在尝试Map路径中的节点。我希望能够拉一棵深度不确定的树,并将其Map到java实体中。
此查询:

match p=(a:fakea)-[*]->(:fakeb) where a.aId = 1
return p;

返回两条路径:

{"start":"http://localhost:8180/db/data/node/593222","nodes":["http://localhost:8180/db/data/node/593222","http://localhost:8180/db/data/node/593223","http://localhost:8180/db/data/node/593224","http://localhost:8180/db/data/node/593225"],"length":3,"relationships":["http://localhost:8180/db/data/relationship/2489542","http://localhost:8180/db/data/relationship/2489543","http://localhost:8180/db/data/relationship/2489544"],"end":"http://localhost:8180/db/data/node/593225"}

{"start":"http://localhost:8180/db/data/node/593222","nodes":["http://localhost:8180/db/data/node/593222","http://localhost:8180/db/data/node/593223","http://localhost:8180/db/data/node/593226","http://localhost:8180/db/data/node/593227"],"length":3,"relationships":["http://localhost:8180/db/data/relationship/2489542","http://localhost:8180/db/data/relationship/2489545","http://localhost:8180/db/data/relationship/2489546"],"end":"http://localhost:8180/db/data/node/593227"}

我试着用我在这里找到的信息用几种不同的方式来Map它:
带ne04j错误的spring数据…检索路径时出错
@query spring数据neo4j中的shortestpath返回类型
我的当前存储库:

public interface FakeRepository extends GraphRepository<FakeA> {

@Query("match p=(a:fakea)-[*]->(:fakeb) where a.aId = {0} return p;")
public EntityPath<FakeA, FakeB> getTree(Long aId);

我还尝试创建一个公共抽象类:

public interface FakeRepository extends GraphRepository<FakeAbs> {

@Query("match p=(a:fakea)-[*]->(:fakeb) where a.aId = {0} return p;")
public EntityPath<FakeAbs, FakeAbs> getTree(Long aId);

我检索不到任何有用的数据。我也找不到我列出的帖子中提到的endresult类。我还尝试用结果 Package entitypath(在回购中也是如此)。

Result<EntityPath<FakeAbs, FakeAbs>> path = fr.getTree(1l);
EntityPath<FakeAbs, FakeAbs> first = path.iterator().next();
first.endNode();

加薪:

Null pointer:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at org.springframework.data.neo4j.support.path.ConvertingEntityPath.endNode(ConvertingEntityPath.java:112)

当我尝试检查entitypath结构的任何其他部分(例如length())时,会收到类似的空指针。
如何查询不同深度的树路径结构并将结果Map到正确的节点实体?我特别想要路径中包含的节点。

pdsfdshx

pdsfdshx1#

试试这个
而不是你现在拥有的:

public interface FakeRepository extends GraphRepository<FakeA> {

@Query("match p=(a:fakea)-[*]->(:fakeb) where a.aId = {0} return p;")
public EntityPath<FakeA, FakeB> getTree(Long aId);

用途:

public interface FakeRepository extends GraphRepository<FakeA> {

@Query("start p=node{0} match (p)-[*]->(a:fakea) return a;")
public FakeA getTree(FakeA fakeA);

该查询将获取fakea的一个示例,并找到与其关联的fakea,在本例中假设只有一个匹配项。如果可以有多个,请更改meth方法签名以返回一个集合。
不过,我认为您也太努力了,没有利用spring数据内置的动态查找器。我建议您浏览一下这个不错的教程:spring数据和neo4j入门

相关问题