问题
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