Spring Data Neo4j实体路径映射

时间:2015-01-14 21:26:53

标签: java neo4j spring-data-neo4j

我有一个独立的Neo4j数据库2.1.6。我从一个基于Web的Spring启动项目开始,添加了Spring Data Neo4j 3.2.1 Release。我试图映射路径内的节点。我希望能够提取一个不确定深度的树并将其映射到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"}

我尝试使用我在此处找到的信息以几种不同的方式映射它:

Spring data wth ne04j error...error while retrieving paths

@Query shortestPath return type in Spring Data Neo4j

我当前的存储库:

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类。我也尝试用结果包装EntityPath(也在repo中)。

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())时,我收到类似的空指针。

如何查询不同深度的树路径结构并将结果映射到正确的节点实体?我特别想要路径中包含的节点。

1 个答案:

答案 0 :(得分:0)

试试这个

而不是现在拥有的东西:

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方法签名以返回Set。

但是,我认为你也在努力工作,而不是利用Spring Data内置的动态查找器。我建议你完成这个很好的教程:Getting Started With Spring Data and Neo4J