@@ -1195,8 +1195,8 @@ private NodesAndRelationshipsByIdStatementProvider createNodesAndRelationshipsBy
11951195 return NodesAndRelationshipsByIdStatementProvider .EMPTY ;
11961196 }
11971197 // load first level relationships
1198- final Set <String > relationshipIds = new HashSet <>();
1199- final Set <String > relatedNodeIds = new HashSet <>();
1198+ // final Set<String> relationshipIds = new HashSet<>();
1199+ final Map < String , Set <String >> relationshipsToRelatedNodeIds = new HashMap <>();
12001200
12011201 for (RelationshipDescription relationshipDescription : entityMetaData .getRelationshipsInHierarchy (queryFragments ::includeField )) {
12021202
@@ -1210,14 +1210,14 @@ private NodesAndRelationshipsByIdStatementProvider createNodesAndRelationshipsBy
12101210 .bindAll (usedParameters )
12111211 .fetch ()
12121212 .one ()
1213- .ifPresent (iterateAndMapNextLevel (relationshipIds , relatedNodeIds , relationshipDescription , PropertyPathWalkStep .empty ()));
1213+ .ifPresent (iterateAndMapNextLevel (relationshipsToRelatedNodeIds , relationshipDescription , PropertyPathWalkStep .empty ()));
12141214 }
12151215
1216- return new NodesAndRelationshipsByIdStatementProvider (rootNodeIds , relationshipIds , relatedNodeIds , queryFragments , elementIdOrIdFunction );
1216+ return new NodesAndRelationshipsByIdStatementProvider (rootNodeIds , relationshipsToRelatedNodeIds . keySet (), relationshipsToRelatedNodeIds . values (). stream (). flatMap ( Collection :: stream ). toList () , queryFragments , elementIdOrIdFunction );
12171217 }
12181218
1219- private void iterateNextLevel (Collection <String > nodeIds , RelationshipDescription sourceRelationshipDescription , Set < String > relationshipIds ,
1220- Set <String > relatedNodeIds , PropertyPathWalkStep currentPathStep ) {
1219+ private void iterateNextLevel (Collection <String > nodeIds , RelationshipDescription sourceRelationshipDescription ,
1220+ Map < String , Set <String >> relationshipsToRelatedNodes , PropertyPathWalkStep currentPathStep ) {
12211221
12221222 Neo4jPersistentEntity <?> target = (Neo4jPersistentEntity <?>) sourceRelationshipDescription .getTarget ();
12231223
@@ -1251,32 +1251,42 @@ private void iterateNextLevel(Collection<String> nodeIds, RelationshipDescriptio
12511251 .bindAll (Collections .singletonMap (Constants .NAME_OF_IDS , TemplateSupport .convertToLongIdOrStringElementId (nodeIds )))
12521252 .fetch ()
12531253 .one ()
1254- .ifPresent (iterateAndMapNextLevel (relationshipIds , relatedNodeIds , relationshipDescription , nextPathStep ));
1254+ .ifPresent (iterateAndMapNextLevel (relationshipsToRelatedNodes , relationshipDescription , nextPathStep ));
12551255 }
12561256 }
12571257
12581258 @ NonNull
1259- private Consumer <Map <String , Object >> iterateAndMapNextLevel (Set <String > relationshipIds ,
1260- Set <String > relatedNodeIds ,
1259+ private Consumer <Map <String , Object >> iterateAndMapNextLevel (Map <String , Set <String >> relationshipsToRelatedNodes ,
12611260 RelationshipDescription relationshipDescription ,
12621261 PropertyPathWalkStep currentPathStep ) {
12631262
12641263 return record -> {
1264+
1265+ Map <String , Set <String >> relatedNodesVisited = new HashMap <>(relationshipsToRelatedNodes );
12651266 @ SuppressWarnings ("unchecked" )
12661267 List <String > newRelationshipIds = ((List <Object >) record .get (Constants .NAME_OF_SYNTHESIZED_RELATIONS )).stream ().map (TemplateSupport ::convertIdOrElementIdToString ).toList ();
1267- relationshipIds .addAll (newRelationshipIds );
1268-
12691268 @ SuppressWarnings ("unchecked" )
1270- List <String > newRelatedNodeIds = (( List <Object >) record .get (Constants .NAME_OF_SYNTHESIZED_RELATED_NODES )).stream ().map (TemplateSupport ::convertIdOrElementIdToString ).toList ();
1269+ Set <String > relatedIds = new HashSet <>((( List <Object >) record .get (Constants .NAME_OF_SYNTHESIZED_RELATED_NODES )).stream ().map (TemplateSupport ::convertIdOrElementIdToString ).toList () );
12711270
1272- Set <String > relatedIds = new HashSet <>(newRelatedNodeIds );
12731271 // use this list to get down the road
12741272 // 1. remove already visited ones;
1275- relatedIds .removeAll (relatedNodeIds );
1276- relatedNodeIds .addAll (relatedIds );
1273+ // we don't know which id came with which node, so we need to assume that a relationshipId connects to all related nodes
1274+ for (String newRelationshipId : newRelationshipIds ) {
1275+ relatedNodesVisited .put (newRelationshipId , relatedIds );
1276+ Set <String > knownRelatedNodesBefore = relationshipsToRelatedNodes .get (newRelationshipId );
1277+ if (knownRelatedNodesBefore != null ) {
1278+ Set <String > mergedKnownRelatedNodes = new HashSet <>(knownRelatedNodesBefore );
1279+ // there are already existing nodes in there for this relationship
1280+ mergedKnownRelatedNodes .addAll (relatedIds );
1281+ relatedNodesVisited .put (newRelationshipId , mergedKnownRelatedNodes );
1282+ relatedIds .removeAll (knownRelatedNodesBefore );
1283+ }
1284+ }
1285+
1286+ relationshipsToRelatedNodes .putAll (relatedNodesVisited );
12771287 // 2. for the rest start the exploration
12781288 if (!relatedIds .isEmpty ()) {
1279- iterateNextLevel (relatedIds , relationshipDescription , relationshipIds , relatedNodeIds , currentPathStep );
1289+ iterateNextLevel (relatedIds , relationshipDescription , relationshipsToRelatedNodes , currentPathStep );
12801290 }
12811291 };
12821292 }
0 commit comments