@@ -1202,8 +1202,8 @@ private NodesAndRelationshipsByIdStatementProvider createNodesAndRelationshipsBy
12021202 return NodesAndRelationshipsByIdStatementProvider .EMPTY ;
12031203 }
12041204 // load first level relationships
1205- final Set <String > relationshipIds = new HashSet <>();
1206- final Set <String > relatedNodeIds = new HashSet <>();
1205+ // final Set<String> relationshipIds = new HashSet<>();
1206+ final Map < String , Set <String >> relationshipsToRelatedNodeIds = new HashMap <>();
12071207
12081208 for (RelationshipDescription relationshipDescription : entityMetaData .getRelationshipsInHierarchy (queryFragments ::includeField )) {
12091209
@@ -1217,14 +1217,14 @@ private NodesAndRelationshipsByIdStatementProvider createNodesAndRelationshipsBy
12171217 .bindAll (usedParameters )
12181218 .fetch ()
12191219 .one ()
1220- .ifPresent (iterateAndMapNextLevel (relationshipIds , relatedNodeIds , relationshipDescription , PropertyPathWalkStep .empty ()));
1220+ .ifPresent (iterateAndMapNextLevel (relationshipsToRelatedNodeIds , relationshipDescription , PropertyPathWalkStep .empty ()));
12211221 }
12221222
1223- return new NodesAndRelationshipsByIdStatementProvider (rootNodeIds , relationshipIds , relatedNodeIds , queryFragments , elementIdOrIdFunction );
1223+ return new NodesAndRelationshipsByIdStatementProvider (rootNodeIds , relationshipsToRelatedNodeIds . keySet (), relationshipsToRelatedNodeIds . values (). stream (). flatMap ( Collection :: stream ). toList () , queryFragments , elementIdOrIdFunction );
12241224 }
12251225
1226- private void iterateNextLevel (Collection <String > nodeIds , RelationshipDescription sourceRelationshipDescription , Set < String > relationshipIds ,
1227- Set <String > relatedNodeIds , PropertyPathWalkStep currentPathStep ) {
1226+ private void iterateNextLevel (Collection <String > nodeIds , RelationshipDescription sourceRelationshipDescription ,
1227+ Map < String , Set <String >> relationshipsToRelatedNodes , PropertyPathWalkStep currentPathStep ) {
12281228
12291229 Neo4jPersistentEntity <?> target = (Neo4jPersistentEntity <?>) sourceRelationshipDescription .getTarget ();
12301230
@@ -1258,32 +1258,42 @@ private void iterateNextLevel(Collection<String> nodeIds, RelationshipDescriptio
12581258 .bindAll (Collections .singletonMap (Constants .NAME_OF_IDS , TemplateSupport .convertToLongIdOrStringElementId (nodeIds )))
12591259 .fetch ()
12601260 .one ()
1261- .ifPresent (iterateAndMapNextLevel (relationshipIds , relatedNodeIds , relationshipDescription , nextPathStep ));
1261+ .ifPresent (iterateAndMapNextLevel (relationshipsToRelatedNodes , relationshipDescription , nextPathStep ));
12621262 }
12631263 }
12641264
12651265 @ NonNull
1266- private Consumer <Map <String , Object >> iterateAndMapNextLevel (Set <String > relationshipIds ,
1267- Set <String > relatedNodeIds ,
1266+ private Consumer <Map <String , Object >> iterateAndMapNextLevel (Map <String , Set <String >> relationshipsToRelatedNodes ,
12681267 RelationshipDescription relationshipDescription ,
12691268 PropertyPathWalkStep currentPathStep ) {
12701269
12711270 return record -> {
1271+
1272+ Map <String , Set <String >> relatedNodesVisited = new HashMap <>(relationshipsToRelatedNodes );
12721273 @ SuppressWarnings ("unchecked" )
12731274 List <String > newRelationshipIds = ((List <Object >) record .get (Constants .NAME_OF_SYNTHESIZED_RELATIONS )).stream ().map (TemplateSupport ::convertIdOrElementIdToString ).toList ();
1274- relationshipIds .addAll (newRelationshipIds );
1275-
12761275 @ SuppressWarnings ("unchecked" )
1277- List <String > newRelatedNodeIds = (( List <Object >) record .get (Constants .NAME_OF_SYNTHESIZED_RELATED_NODES )).stream ().map (TemplateSupport ::convertIdOrElementIdToString ).toList ();
1276+ Set <String > relatedIds = new HashSet <>((( List <Object >) record .get (Constants .NAME_OF_SYNTHESIZED_RELATED_NODES )).stream ().map (TemplateSupport ::convertIdOrElementIdToString ).toList () );
12781277
1279- Set <String > relatedIds = new HashSet <>(newRelatedNodeIds );
12801278 // use this list to get down the road
12811279 // 1. remove already visited ones;
1282- relatedIds .removeAll (relatedNodeIds );
1283- relatedNodeIds .addAll (relatedIds );
1280+ // we don't know which id came with which node, so we need to assume that a relationshipId connects to all related nodes
1281+ for (String newRelationshipId : newRelationshipIds ) {
1282+ relatedNodesVisited .put (newRelationshipId , relatedIds );
1283+ Set <String > knownRelatedNodesBefore = relationshipsToRelatedNodes .get (newRelationshipId );
1284+ if (knownRelatedNodesBefore != null ) {
1285+ Set <String > mergedKnownRelatedNodes = new HashSet <>(knownRelatedNodesBefore );
1286+ // there are already existing nodes in there for this relationship
1287+ mergedKnownRelatedNodes .addAll (relatedIds );
1288+ relatedNodesVisited .put (newRelationshipId , mergedKnownRelatedNodes );
1289+ relatedIds .removeAll (knownRelatedNodesBefore );
1290+ }
1291+ }
1292+
1293+ relationshipsToRelatedNodes .putAll (relatedNodesVisited );
12841294 // 2. for the rest start the exploration
12851295 if (!relatedIds .isEmpty ()) {
1286- iterateNextLevel (relatedIds , relationshipDescription , relationshipIds , relatedNodeIds , currentPathStep );
1296+ iterateNextLevel (relatedIds , relationshipDescription , relationshipsToRelatedNodes , currentPathStep );
12871297 }
12881298 };
12891299 }
0 commit comments