Skip to content

Commit e3749e4

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 71396db commit e3749e4

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ boolean hasRootNodeIds() {
181181
}
182182

183183
Statement toStatement() {
184-
185184
String rootNodeIds = "rootNodeIds";
186185
String relationshipIds = "relationshipIds";
187186
String relatedNodeIds = "relatedNodeIds";
@@ -190,14 +189,20 @@ Statement toStatement() {
190189
Relationship relationships = Cypher.anyNode().relationshipBetween(Cypher.anyNode()).named(relationshipIds);
191190
return Cypher.match(rootNodes)
192191
.where(Functions.id(rootNodes).in(Cypher.parameter(rootNodeIds)))
192+
.with(Functions.collect(rootNodes).as("n"))
193193
.optionalMatch(relationships)
194194
.where(Functions.id(relationships).in(Cypher.parameter(relationshipIds)))
195+
.with(Constants.NAME_OF_ROOT_NODE, Functions.collectDistinct(relationships).as(Constants.NAME_OF_SYNTHESIZED_RELATIONS).asExpression())
195196
.optionalMatch(relatedNodes)
196197
.where(Functions.id(relatedNodes).in(Cypher.parameter(relatedNodeIds)))
197-
.with(
198-
rootNodes.as(Constants.NAME_OF_ROOT_NODE.getValue()),
199-
Functions.collectDistinct(relationships).as(Constants.NAME_OF_SYNTHESIZED_RELATIONS),
200-
Functions.collectDistinct(relatedNodes).as(Constants.NAME_OF_SYNTHESIZED_RELATED_NODES))
198+
.with(Constants.NAME_OF_ROOT_NODE,
199+
Cypher.name(Constants.NAME_OF_SYNTHESIZED_RELATIONS).as(Constants.NAME_OF_SYNTHESIZED_RELATIONS),
200+
Functions.collectDistinct(relatedNodes).as(Constants.NAME_OF_SYNTHESIZED_RELATED_NODES).asExpression()
201+
)
202+
.unwind(Constants.NAME_OF_ROOT_NODE).as(rootNodeIds)
203+
.with(Cypher.name(rootNodeIds).as(Constants.NAME_OF_ROOT_NODE).asExpression(),
204+
Cypher.name(Constants.NAME_OF_SYNTHESIZED_RELATIONS),
205+
Cypher.name(Constants.NAME_OF_SYNTHESIZED_RELATED_NODES))
201206
.orderBy(queryFragments.getOrderBy())
202207
.returning(
203208
Constants.NAME_OF_ROOT_NODE.as(Constants.NAME_OF_SYNTHESIZED_ROOT_NODE),

0 commit comments

Comments
 (0)