2015-01-14 94 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