@@ -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