How do I query for relationship data in spring data neo4j 4?

落花浮王杯 提交于 2020-01-05 09:28:11

问题


I have a cypher query that is supposed to return nodes and edges so that I can render a representation of my graph in a web app. I'm running it with the query method in Neo4jOperations.

start n=node({id}) match n-[support:SUPPORTED_BY|INTERPRETS*0..5]->(argument:ArgumentNode)
return argument, support

Earlier, I was using spring data neo4j 3.3.1 with an embedded database, and this query did a fine job of returning relationship proxies with start nodes and end nodes. I've upgraded to spring data neo4j 4.0.0 and switched to using a remote server, and now it returns woefully empty LinkedHashMaps.

This is the json response from the server:

{"commit":"http://localhost:7474/db/data/transaction/7/commit","results":[{"columns":["argument","support"],
    "data":[
        {"row":[{"buildVersion":-1},[]]},
        {"row":[{"buildVersion":-1},[{}]]}
    ]}],"transaction":{"expires":"Mon, 12 Oct 2015 06:49:12 +0000"},"errors":[]}

I obtained this json by putting a breakpoint in DefaultRequest.java and executing EntityUtils.toString(response.getEntity()). The query is supposed to return two nodes which are related via an edge of type INTERPRETS. In the response you see [{}], which is where data about the edge should be.

How do I get a response with the data I need?


回答1:


Disclaimer: this is not a definitive answer, just what I've pieced together so far.

You can use the queryForObjects method in Neo4jOperations, and make sure that your query returns a path. Example:

neo4jOperations.queryForObjects(ArgumentNode.class, "start n=node({id}) match path=n-[support:SUPPORTED_BY|INTERPRETS*0..5]->(argument:ArgumentNode) return path", params);

The POJOs that come back should be hooked together properly based on their relationship annotations. Now you can poke through them and manually build a set of edges that you can serialize. Not ideal, but workable.

Docs suggesting that you return a path:

From http://docs.spring.io/spring-data/data-neo4j/docs/4.0.0.RELEASE/reference/html/#_cypher_queries:

For the query methods that retrieve mapped objects, the recommended query format is to return a path, which should ensure that known types get mapped correctly and joined together with relationships as appropriate.

Explanation of why queryForObjects helps:

Under the hood, there is a distinction between different types of queries. They have GraphModelQuery, RowModelQuery, and GraphRowModelQuery, each of which pass a different permutation of resultDataContents: ["row", "graph"] to the server. If you want data sufficient to reconstruct the graph, you need to make sure "graph" is in the list.

You can find this code inside ExecuteQueriesDelegate:

if (type != null && session.metaData().classInfo(type.getSimpleName()) != null) {
    Query qry = new GraphModelQuery(cypher, parameters);
    ...
} else {
    RowModelQuery qry = new RowModelQuery(cypher, parameters);
    ...
}

Using queryForObjects allows you to provide a type, and kicks things over into GraphModelQuery mode.



来源:https://stackoverflow.com/questions/33075364/how-do-i-query-for-relationship-data-in-spring-data-neo4j-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!