Showing results for 
Search instead for 
Did you mean: 

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

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.


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

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

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")

The testcase succeeds but only leaves these these nodes and relationships in Neo4j database:

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:

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.



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

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
| -
I'll try to upgrade and try again

Upgrading fixed the problem. Thanks a lot.