Skip to content

Commit c14614e

Browse files
committed
feat: update neo4j
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
1 parent ed6dcf8 commit c14614e

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

jnosql-neo4j/src/main/java/org/eclipse/jnosql/databases/neo4j/communication/DefaultNeo4JDatabaseManager.java

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -328,37 +328,45 @@ public CommunicationEdge edge(CommunicationEntity source, String label, Communic
328328
source = ensureEntityExists(source);
329329
target = ensureEntityExists(target);
330330

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+
346336
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();
351337

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(
353345
"sourceElementId", sourceId,
354-
"targetElementId", targetId,
355-
"props", properties
346+
"targetElementId", targetId
356347
));
357348

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;
361350

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();
362370
return new Neo4jCommunicationEdge(relationship.elementId(), source, target, label, properties);
363371
}
364372
}

0 commit comments

Comments
 (0)