1、報錯信息
在使用spring data neo4j 查詢neo4j中的數據時,我用了自定義的查詢語句,即
@Query(value = "match (n:NodeType) - [r:RELATION_TYPE] -> (m:NodeType) where n.id=$0 return r") List<RelationType> findRelation(Integer id);
執行程序后,報錯如下:
Relationship (30)-[RELATION_TYPE]->(29) cannot be fully hydrated because one or more required node entities have not been part of the result set.
報錯信息看得我很迷,不知所雲。。。
2、原因&解決方案
經過一波面向谷歌編程后,=_=!我悟了!
原來是我自定義的neo4j查詢語句出現了問題,“return”字段后應該返回兩端節點和邊,如下:
@Query(value = "match (n:NodeType) - [r:RELATION_TYPE] -> (m:NodeType) where n.id=$0 return n, r, m")
// 或者 @Query(value = "match p=(n:NodeType) - [r:RELATION_TYPE] -> (m:NodeType) where n.id=$0 return p")
List<RelationType> findRelation(Integer id);
這樣看來,前面的報錯信息可能的意思是:
如果我要查詢neo4j中的邊,需要在neo4j語句中不只返回邊,還要返回兩端節點。這樣我們查詢方法 findRelation(...) 返回的結果集就是 neo4j語句返回結果的一部分了。