@@ -328,37 +328,45 @@ public CommunicationEdge edge(CommunicationEntity source, String label, Communic
328
328
source = ensureEntityExists (source );
329
329
target = ensureEntityExists (target );
330
330
331
- String cypher = "MATCH (s) WHERE elementId(s) = $sourceElementId " +
332
- "MATCH (t) WHERE elementId(t) = $targetElementId " +
333
- "OPTIONAL MATCH (s)-[existing:" + label + "]->(t) " +
334
- "WITH s, t, existing " +
335
- "CALL { " +
336
- " WITH s, t, existing " +
337
- " WHERE existing IS NULL " +
338
- " CREATE (s)-[r:" + label + " $props]->(t) " +
339
- " RETURN r " +
340
- " UNION " +
341
- " RETURN existing AS r " +
342
- "} " +
343
- "RETURN r" ;
344
-
345
- LOGGER .fine (() -> "Creating edge with Cypher query: " + cypher );
331
+ var sourceId = source .find (ID ).orElseThrow (() ->
332
+ new EdgeCommunicationException ("The source entity should have the " + ID + " property" )).get ();
333
+ var targetId = target .find (ID ).orElseThrow (() ->
334
+ new EdgeCommunicationException ("The target entity should have the " + ID + " property" )).get ();
335
+
346
336
try (Transaction tx = session .beginTransaction ()) {
347
- var sourceId = source .find (ID ).orElseThrow (() ->
348
- new EdgeCommunicationException ("The source entity should have the " + ID + " property" )).get ();
349
- var targetId = target .find (ID ).orElseThrow (() ->
350
- new EdgeCommunicationException ("The target entity should have the " + ID + " property" )).get ();
351
337
352
- var result = tx .run (cypher , Values .parameters (
338
+ String findEdge = "MATCH (s) WHERE elementId(s) = $sourceElementId " +
339
+ "MATCH (t) WHERE elementId(t) = $targetElementId " +
340
+ "MATCH (s)-[r:" + label + "]->(t) RETURN r" ;
341
+
342
+ LOGGER .fine (() -> "Finding existing edge with ID: " + sourceId + " to " + targetId );
343
+ LOGGER .fine (() -> "Cypher Query: " + findEdge );
344
+ var result = tx .run (findEdge , Values .parameters (
353
345
"sourceElementId" , sourceId ,
354
- "targetElementId" , targetId ,
355
- "props" , properties
346
+ "targetElementId" , targetId
356
347
));
357
348
358
- var relationship = result .single ().get ("r" ).asRelationship ();
359
- LOGGER .fine (() -> "Created edge with ID: " + relationship .elementId ());
360
- tx .commit ();
349
+ org .neo4j .driver .types .Relationship relationship ;
361
350
351
+ if (result .hasNext ()) {
352
+ relationship = result .single ().get ("r" ).asRelationship ();
353
+ LOGGER .fine (() -> "Found existing edge with ID: " + relationship .elementId ());
354
+ } else {
355
+ String createEdge = "MATCH (s) WHERE elementId(s) = $sourceElementId " +
356
+ "MATCH (t) WHERE elementId(t) = $targetElementId " +
357
+ "CREATE (s)-[r:" + label + " $props]->(t) RETURN r" ;
358
+
359
+ LOGGER .fine (() -> "Creating new edge with ID: " + sourceId + " to " + targetId );
360
+ LOGGER .fine (() -> "Cypher Query: " + createEdge );
361
+ var createResult = tx .run (createEdge , Values .parameters (
362
+ "sourceElementId" , sourceId ,
363
+ "targetElementId" , targetId ,
364
+ "props" , properties
365
+ ));
366
+ relationship = createResult .single ().get ("r" ).asRelationship ();
367
+ LOGGER .fine (() -> "Created new edge with ID: " + relationship .elementId ());
368
+ }
369
+ tx .commit ();
362
370
return new Neo4jCommunicationEdge (relationship .elementId (), source , target , label , properties );
363
371
}
364
372
}
0 commit comments