cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

SDN only saves the last relationship when having multiple relationhips to the same target node

jaboj78
Node Link

Using SDN 6.

When creating two nodes with multiple relationships of the same type, only the last relationship, in the list of relationships, survives the save method of the neo4Tempate. According to the cypher debug it also seems like, SDN during the save deletes all existing relationships, which also is weird.

Code:

@Node
@Data @With @AllArgsConstructor @NoArgsConstructor
public class Person {
	@Id @GeneratedValue
    private Long Id;
	private String name;
	@Relationship(type = "CALLS", direction = Direction.OUTGOING) 
	private List<Calls> calls;
}

@RelationshipProperties
@Data @With @AllArgsConstructor @RequiredArgsConstructor
public class Calls {
	@Id @GeneratedValue
    private Long id;
	@TargetNode
    private final Person target;
	private String day;
}

@Test
public void create_persons_calls() {
	Person p1 = new Person().withName("Person-1");
	Person p2 = new Person().withName("Person-2");
  	p1.setCalls(List.of(new Calls(p2).withDay("monday")
  			           ,new Calls(p2).withDay("tuesday ")
  			           ,new Calls(p2).withDay("wednesday")
  			   ));
  	this.neoTemplate.save(p1);
}

The testcase succeeds but only leaves these these nodes and relationships in Neo4j database:
3X_5_e_5ed6f3f6adaf906f94ca9f8de9c1c69ff6e39450.png

Looking at the debug output a sequence of statements appear. In between a few DELETE statements, which apparently deletes the relationships just created, but why???

MATCH (startNode)-[rel:`CALLS`]->(:`Person`) WHERE id(startNode) = $fromId DELETE rel

Its odd, because the the CALLS are all unique. Creating the same relationships manually is allowed in Neo4J:

CREATE (s:Person{name: "Person-1"})
CREATE (t:Person{name: "Person-2"})
CREATE (s)-[:CALLS]->(t)
CREATE (s)-[:CALLS]->(t)
CREATE (s)-[:CALLS]->(t)

And will create this:
3X_1_c_1c5788b45948a5d68e9c0033280facda60391282.png

As a workaround, it's actually possible to create multiple CALLS manually by creating a Neo4JRepository and add a "addCalls" method with a @Query annotation with an appropriate cypher query. But this makes object mapping in SDN useless and I'd like to create the relationships using the object model

Why isn't it possible to create multiple relationships of the same type between two nodes using the object model and the save methods of the neo4jTemplate, according to the example in the top?

I'm looking forward for an explanation as I need a fix for this.

3 REPLIES 3

gerrit_meier
Neo4j
Neo4j

Which version are you using?
I created an example with the latest minor version (6.1.5) of SDN 6.1.x.

and I see
3X_1_b_1b69054c4583eadd3fbd4e321e30756b68dbfa48.png

Oh, actually I'm only on version 6.0.1, which might be the the problem:
+- org.springframework.boot:spring-boot-starter-data-neo4j:jar:2.4.0:compile
| - org.springframework.data:spring-data-neo4j:jar:6.0.1:compile
I'll try to upgrade and try again

Upgrading fixed the problem. Thanks a lot.