I'm using Spring Data Neo4j 4 (SDN4), it seem that when I try to "modify" the one-to-one relationship attribute, the SDN will "add" it in the database in a particular operation way
Codes (skip encapsulation)
@NoteEntity
public class Bar{
String name;
}
@NoteEntity
public class Foo {
String name;
@Relationship(type="BAR1", direction=Relationship.INCOMING)
Bar b1;
@Relationship(type="BAR2", direction=Relationship.OUTGOING)
Bar b2;
}
And then test it with
@Test
public void test_add(){
Foo foo1 = new Foo("Foo1");
Bar bar1 = new Bar("Bar1");
Bar bar2 = new Bar("Bar2");
foo.b1 = bar1;
foo.b2 = bar2;
fooRepository.save(foo);
}
If you go to the Neo4j query browser, you will see (Foo1)-[:BAR1]->(Bar1), and (Foo1)-[:BAR2]->(Bar2)
And here is the most important action : restart your spring container and run this follow Test (do not run it in the same time)
@Test
public void test_update(){
Foo foo1 = fooRepository.findAll().iterator().next();
Bar bar3 = new Bar("Bar3");
foo.b1 = bar3;
fooRepository.save(foo);
}
If you go to the Neo4j query browser, you will see 3 relation (Foo1)-[:BAR1]->(Bar1) and (Foo1)-[:BAR1]->(Bar3) and (Foo1)-[:BAR2]->(Bar2) The original relationship(Bar1) is not deleted.
It seems once the spring container (or Neo4j session) is restarted, the one-to-one relationship will totally go wrong.
The current work around is the change all the relationship to become one-to-many like @NoteEntity public class Foo {
String name;
@Relationship(type="BAR1", direction=Relationship.INCOMING)
List<Bar> b1;
@Relationship(type="BAR2", direction=Relationship.OUTGOING)
List<Bar> b2;
}
Is there any other way to fix it?