2015-01-14 92 views
0

我有一个独立的Neo4j数据库2.1.6。我从一个基于Web的Spring启动项目开始,并添加了Spring Data Neo4j 3.2.1版本。我试图映射路径内的节点。我希望能够拉出一个不确定深度的树并将其映射到Java实体中。Spring数据Neo4j实体路径映射

此查询:

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(在回购中)。

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结构(长度()例如)的任何其它部分。

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

+0

你想获得路径信息还是想要在路径中标识特定节点? –

+0

我正在尝试检索路径中的特定节点。为了清晰起见,我将编辑我的原始问题。 – jake

+0

转到此链接的解决方案: https://stackoverflow.com/questions/32962032/how-do-i-query-for-paths-in-spring-data-neo4j-4?rq=1 – joe

回答

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

+0

解决方案我最终使用的是返回一个对象并映射我使用Relatedto和Fetch注释所需的关系。自那以后,我发现这是实现这一点的较慢方法之一。 http://jexp.de/blog/2014/12/spring-data-neo4j-improving-remoting-performance/ – jake