0

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?

Luanne
  • 19,145
  • 1
  • 39
  • 51
minglight
  • 87
  • 6
  • Do you have any getters/setters in Foo? Are they annotated? – Luanne Mar 29 '16 at 03:28
  • Yes, the standard encapsulation in Foo. And the getter/setter do not have any annotation. – minglight Mar 29 '16 at 08:23
  • Incoming relationships must have annotations on any fields or methods backing it. Could you try annotating the methods and see if the issue is fixed? – Luanne Mar 29 '16 at 08:29
  • Do you mean in the Bar I should put an Outgoing relationship reference of Foo? Also I didn't see any document mentioned about to put the annotation on method, do you have any example? – minglight Mar 29 '16 at 09:20
  • No, I mean put the @Relationship(type="BAR1",direction="INCOMING") on the getter and setter for b1 in class Foo. See last para here- http://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#__relationship_connecting_node_entities – Luanne Mar 29 '16 at 09:51
  • The annotation on method doesn't work. This issue "only" happens when the context restart. It seems the neo4j context doesn't recognize the relationship is there, and it will add the relationship it again instead of updating it. – minglight Mar 30 '16 at 10:02
  • Could you please submit a test to https://github.com/neo4j/neo4j-ogm/issues and we'll take a look – Luanne Mar 30 '16 at 10:13

0 Answers0