Map arbitrary relations via Neo4j OGM

眉间皱痕 提交于 2020-01-24 16:27:05

问题


Neo4j OGM supports arbitrary node properties being mapped to entity classes via @Convert and CompositeAttributeConverter but what about the relation part? How would I map arbitrary relations in a @NodeEntity?

The actual properties and relations being used in my data model are configurable, i.e. they are not known at compile time. Example: Configuration specifies nodes of label A and B and a relation from A to B named REL_1. Now when I am querying nodes of label A then I would like to retrieve and map the relations in the corresponding resulting node entity.

I tried to build cypher queries depending on the configuration but I am stuck at retrieving relations whose end node should be mapped by OGM. Here is my simplified generated query structure:

MATCH (n:A) RETURN n, [(n)-[rn:REL_1]-(n2) | [rn, n2]]

I also tried

MATCH (n:A) RETURN n, [[(n)-[rn:REL_1]-(n2) | [rn, n2]]]

which is the same pattern as is generated by OGM when calling session.load with a fixed data model. However, both queries do not map n2 in the result of session.query call. Instead the returned type for n2 is a NodeModel but I don't want to map it myself because OGM is already capable to do it. What's wrong with my query? Or is this a bug?

Note: This query would do the trick but that feels like an odd workaround.

MATCH (n:A) RETURN n, [(n)-[rn:REL_1]-() | rn], [(n)-[:REL_1]-(n2) | n2]

I am using Neo4J 3.5 with OGM 3.2.3 included via Sprint Boot 2.2.2.


回答1:


This could be due to an idiosyncrasy of the pattern comprehension implementation.

Does this more straightforward Cypher query work for you?

MATCH (n:A)-[rn:REL_1]-(n2) RETURN n, COLLECT({rn: rn, n2: n2})


来源:https://stackoverflow.com/questions/59481997/map-arbitrary-relations-via-neo4j-ogm

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