Skip to content

Commit dc27d5c

Browse files
committed
GH-2387 - Improve Cypher statement for cycles.
This commit introduces a change to the Cypher statement issued if a cyclic domain is queried. Closes #2387
1 parent be15af9 commit dc27d5c

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/main/java/org/springframework/data/neo4j/core/TemplateSupport.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,22 @@ Statement toStatement(NodeDescription<?> nodeDescription) {
190190
Relationship relationships = Cypher.anyNode().relationshipBetween(Cypher.anyNode()).named(relationshipIds);
191191
return Cypher.match(rootNodes)
192192
.where(Functions.id(rootNodes).in(Cypher.parameter(rootNodeIds)))
193+
.with(Functions.collect(rootNodes).as(Constants.NAME_OF_ROOT_NODE))
193194
.optionalMatch(relationships)
194195
.where(Functions.id(relationships).in(Cypher.parameter(relationshipIds)))
196+
.with(Constants.NAME_OF_ROOT_NODE, Functions.collectDistinct(relationships).as(Constants.NAME_OF_SYNTHESIZED_RELATIONS).asExpression())
195197
.optionalMatch(relatedNodes)
196198
.where(Functions.id(relatedNodes).in(Cypher.parameter(relatedNodeIds)))
197199
.with(
198-
rootNodes.as(Constants.NAME_OF_TYPED_ROOT_NODE.apply(nodeDescription).getValue()),
199-
Functions.collectDistinct(relationships).as(Constants.NAME_OF_SYNTHESIZED_RELATIONS),
200-
Functions.collectDistinct(relatedNodes).as(Constants.NAME_OF_SYNTHESIZED_RELATED_NODES))
200+
Constants.NAME_OF_ROOT_NODE,
201+
Cypher.name(Constants.NAME_OF_SYNTHESIZED_RELATIONS).as(Constants.NAME_OF_SYNTHESIZED_RELATIONS),
202+
Functions.collectDistinct(relatedNodes).as(Constants.NAME_OF_SYNTHESIZED_RELATED_NODES).asExpression()
203+
)
204+
.unwind(Constants.NAME_OF_ROOT_NODE).as(rootNodeIds)
205+
.with(
206+
Cypher.name(rootNodeIds).as(Constants.NAME_OF_TYPED_ROOT_NODE.apply(nodeDescription).getValue()).asExpression(),
207+
Cypher.name(Constants.NAME_OF_SYNTHESIZED_RELATIONS),
208+
Cypher.name(Constants.NAME_OF_SYNTHESIZED_RELATED_NODES))
201209
.orderBy(queryFragments.getOrderBy())
202210
.returning(
203211
Constants.NAME_OF_TYPED_ROOT_NODE.apply(nodeDescription).as(Constants.NAME_OF_SYNTHESIZED_ROOT_NODE),

0 commit comments

Comments
 (0)