将 neo4j ogm 查询结果映射到 java 对象 [英] Mapping neo4j ogm query results to java objects

查看:164
本文介绍了将 neo4j ogm 查询结果映射到 java 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 Neo4j 数据库收集信息,但我返回的值是从多个节点中挑选出来的,所以我基本上返回的是一个带有一些属性的表.在这个例子中,假设我返回属性 color:Stringname:Stringcount:String.我使用 session.query(*QUERY*, queryParams) 查询这些结果.现在,当我得到结果时,我想将它映射到一个现有的 Java 对象,我创建它来保存这些数据.这与正常"映射有点不同,因为一般来说,您希望将图形节点映射到代表这些节点的对象.在这里,我的 POJO 与图节点无关.我设法为我的每个数据对象使用自定义 CompositeAttributeConverter 类来做到这一点,但我觉得必须有比为每个新对象编写一个新类更好的解决方案.

I'm collecting infos from a neo4j db, but the values I return are picked out from multiple nodes, so what I'm basically returning is a table with some properties. For this example, let's say I return the properties color:String, name:String, count:String. I query these results using session.query(*QUERY*, queryParams). Now, when I get the results, I want to map it to an existing Java Object, that I created to hold this data. This is kind of different to the 'normal' mapping, as in general, you want to map your graph nodes to objects that represent those nodes. Here, my POJOs have nothing to do with the graph nodes. I managed to do this using custom CompositeAttributeConverter classes for each of my data-objects, but I feel there must be a better solution than writing a new class for every new object.

推荐答案

你可能想看看 使用 Session 对象执行任意 Cypher 查询.您可以从返回的 Result 对象中获取 Iterable,您可以对其进行处理或仅输出到 Map 结果的集合.

You might want to take a look at executing arbitrary Cypher queries using the Session object. You can get an Iterable<Map<String,Object>> from the returned Result object, which you could process over or just output to a collection of Map results.

或者,如果您安装了 APOC 程序,您可以随时编写一个查询以将结果作为 JSON 字符串返回,并使用适当的库将其转换为 Java 中的 JSON 对象,并根据需要使用这些对象.

Or, if you have APOC Procedures installed, you can always write up a query to return your results as a JSON string, and convert that to JSON objects in Java with the appropriate library and use those as needed.

这篇关于将 neo4j ogm 查询结果映射到 java 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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