@@ -1202,8 +1202,8 @@ private NodesAndRelationshipsByIdStatementProvider createNodesAndRelationshipsBy
1202
1202
return NodesAndRelationshipsByIdStatementProvider .EMPTY ;
1203
1203
}
1204
1204
// 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 <>();
1207
1207
1208
1208
for (RelationshipDescription relationshipDescription : entityMetaData .getRelationshipsInHierarchy (queryFragments ::includeField )) {
1209
1209
@@ -1217,14 +1217,14 @@ private NodesAndRelationshipsByIdStatementProvider createNodesAndRelationshipsBy
1217
1217
.bindAll (usedParameters )
1218
1218
.fetch ()
1219
1219
.one ()
1220
- .ifPresent (iterateAndMapNextLevel (relationshipIds , relatedNodeIds , relationshipDescription , PropertyPathWalkStep .empty ()));
1220
+ .ifPresent (iterateAndMapNextLevel (relationshipsToRelatedNodeIds , relationshipDescription , PropertyPathWalkStep .empty ()));
1221
1221
}
1222
1222
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 );
1224
1224
}
1225
1225
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 ) {
1228
1228
1229
1229
Neo4jPersistentEntity <?> target = (Neo4jPersistentEntity <?>) sourceRelationshipDescription .getTarget ();
1230
1230
@@ -1258,32 +1258,42 @@ private void iterateNextLevel(Collection<String> nodeIds, RelationshipDescriptio
1258
1258
.bindAll (Collections .singletonMap (Constants .NAME_OF_IDS , TemplateSupport .convertToLongIdOrStringElementId (nodeIds )))
1259
1259
.fetch ()
1260
1260
.one ()
1261
- .ifPresent (iterateAndMapNextLevel (relationshipIds , relatedNodeIds , relationshipDescription , nextPathStep ));
1261
+ .ifPresent (iterateAndMapNextLevel (relationshipsToRelatedNodes , relationshipDescription , nextPathStep ));
1262
1262
}
1263
1263
}
1264
1264
1265
1265
@ 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 ,
1268
1267
RelationshipDescription relationshipDescription ,
1269
1268
PropertyPathWalkStep currentPathStep ) {
1270
1269
1271
1270
return record -> {
1271
+
1272
+ Map <String , Set <String >> relatedNodesVisited = new HashMap <>(relationshipsToRelatedNodes );
1272
1273
@ SuppressWarnings ("unchecked" )
1273
1274
List <String > newRelationshipIds = ((List <Object >) record .get (Constants .NAME_OF_SYNTHESIZED_RELATIONS )).stream ().map (TemplateSupport ::convertIdOrElementIdToString ).toList ();
1274
- relationshipIds .addAll (newRelationshipIds );
1275
-
1276
1275
@ 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 () );
1278
1277
1279
- Set <String > relatedIds = new HashSet <>(newRelatedNodeIds );
1280
1278
// use this list to get down the road
1281
1279
// 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 );
1284
1294
// 2. for the rest start the exploration
1285
1295
if (!relatedIds .isEmpty ()) {
1286
- iterateNextLevel (relatedIds , relationshipDescription , relationshipIds , relatedNodeIds , currentPathStep );
1296
+ iterateNextLevel (relatedIds , relationshipDescription , relationshipsToRelatedNodes , currentPathStep );
1287
1297
}
1288
1298
};
1289
1299
}
0 commit comments