问题
I'm currently evaluating OGM / Spring Data Neo4j for a use case and came upon the following problem:
When executing a custom Cypher query either via Spring Data @Query
annotation or directly via the Neo4j Session
, the result contains only the nodes directly queried for and not the related nodes (relationships are null
in the resulting node objects). I.e. the depth for these queries seems to be 0 and not 1, as I would have expected from the docs.
How can I execute a custom Cypher query via OGM or Spring Data Neo4j that has depth 1?
回答1:
The default depth 1 refers to findOne/findAll/.. methods from the repository and derived finders.
This is what the documentation says about custom queries:
In the current version, custom queries do not support paging, sorting or a custom depth. In addition, it does not support mapping a path to domain entities, as such, a path should not be returned from a Cypher query. Instead, return nodes and relationships to have them mapped to domain entities.
http://docs.spring.io/spring-data/data-neo4j/docs/current/reference/html/#reference:session:loading-entities:cypher-queries
For example when you have a query
MATCH (n:MyLabel)-[r]-(n2)
WHERE ... // some condition
RETURN n,r,n2
list all nodes/relationships you want to map to your objects in the RETURN clause.
来源:https://stackoverflow.com/questions/43459968/depth-in-custom-cypher-queries-with-ogm-always-0