如何在spring数据neo4j 4中查询关系数据? [英] How do I query for relationship data in spring data neo4j 4?

查看:934
本文介绍了如何在spring数据neo4j 4中查询关系数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个密码查询,该查询应该返回节点和边,以便可以在Web应用程序中呈现图形的表示形式.我正在Neo4jOperations中使用query方法运行它.

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

之前,我使用的是带有嵌入式数据库的spring数据neo4j 3.3.1,该查询在返回具有起始节点和结束节点的关系代理方面做得很好.我已经升级到spring数据neo4j 4.0.0,并切换为使用远程服务器,现在它返回的是悲惨的空LinkedHashMaps.

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.

这是来自服务器的json响应:

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":[]}

我通过在DefaultRequest.java中放置一个断点并执行EntityUtils.toString(response.getEntity())获得了这个json.该查询应该返回通过类型为INTERPRETS的边相关的两个节点.在响应中,您会看到[{}],这是有关边缘的数据应放在的位置.

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?

推荐答案

免责声明:这不是一个明确的答案,只是我到目前为止整理的内容.

您可以在Neo4jOperations中使用queryForObjects方法,并确保查询返回路径.示例:

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);

返回的POJO应该根据它们的关系注释正确地连接在一起.现在,您可以浏览它们并手动构建一组可以序列化的边缘.不理想,但可行.

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.

文档建议您返回路径:

来自 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.

queryForObjects为何起作用的解释:

内幕下,不同类型的查询之间存在区别.它们具有GraphModelQueryRowModelQueryGraphRowModelQuery,它们每个都将resultDataContents: ["row", "graph"]的不同排列传递给服务器.如果您需要足够的数据来重建图形,则需要确保图形"在列表中.

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.

您可以在ExecuteQueriesDelegate中找到此代码:

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);
    ...
}

使用queryForObjects可以提供一种类型,并将其插入GraphModelQuery模式.

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

这篇关于如何在spring数据neo4j 4中查询关系数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆