I am using Spring Data Neo4j 4 . Lets say I have nodes of type Person and the corresponding Java domain object is
@NodeEntity
public class Person {
@GraphId private Long id;
private String name;
private String lastName;
@Relationship(type = "KNOWS")
Set<Person> myTypes;
}
And I am trying to create a graph like `
John-KNOWS->George and John-KNOWS->Ann. Ann-KNOWS->Dug
`. Below is how I am setting up the domain objects and persisting.
Person john = new Person("John","P");
Person george = new Person("George","B");
Person Ann = new Person("Ann","M");
Person Dug = new Person("Dug","S");
Set<Person> Persons= new HashSet<Person>();
Set<Person> Persons1= new HashSet<Person>();
Persons1.add(Dug);
Ann.setMyTypes(Persons1);
Persons.add(george);
Persons.add(Ann);
john.setMyTypes(Persons);
personRepository.save(john);
personRepository.save(george);
personRepository.save(Ann);
personRepository.save(Dug);
But to my surprise only the nodes are created without relationships . I was debugging this and after this line personRepository.save(john) The nodes with relationships got created but during subsequent saves all the relationships got deleted. Below is what is printed in the logs and clearly says that relationships are deleted.
Request: UNWIND {rows} as row CREATE (n:`Person`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, row.type as type with params {rows=[{nodeRef=-2115049587, type=node, props={name=George, attrib=B}}, {nodeRef=-341599918, type=node, props={name=Ann, attrib=M}}, {nodeRef=-737678933, type=node, props={name=John, attrib=P}}, {nodeRef=-1025122203, type=node, props={name=Dug, attrib=S}}]}
Request: UNWIND {rows} as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`KNOWS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, row.type as type with params {rows=[{relRef=-1807300324, endNodeId=2448, type=rel, startNodeId=2450}, {relRef=-272508006, endNodeId=2451, type=rel, startNodeId=2449}, {relRef=-845107952, endNodeId=2449, type=rel, startNodeId=2450}]}
Request: UNWIND {rows} as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`KNOWS`]->(endNode) DELETE rel with params {rows=[{endNodeId=2448, startNodeId=2450}]}
Request: UNWIND {rows} as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`KNOWS`]->(endNode) DELETE rel with params {rows=[{endNodeId=2449, startNodeId=2450}]}
Request: UNWIND {rows} as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`KNOWS`]->(endNode) DELETE rel with params {rows=[{endNodeId=2451, startNodeId=2449}]}
Not sure where I am going wrong or this is how it is supposed to work