title: Neo4j - how to delete relationship with another node on save or update of a related node
body: <p>I am using Neo4j (version 3.4.1) and Spring-data-neo4j (5.0.10.RELEASE) in my application. I am also using OGM.</p>
  
  <p>I have the following domain model (Vehicle and Part):</p>
  
  <p>Vehicle class</p>
  
  <pre><code>@NodeEntity
  @Data
  @NoArgsConstructor
  public class Vehicle {
  
  @Id
  @GeneratedValue
  private Long id;
  
  @Relationship(type = "HAS_PART", direction = Relationship.OUTGOING)
  private List&lt;Part&gt; parts = new ArrayList&lt;&gt;();
  }
  </code></pre>
  
  <p>Part class</p>
  
  <pre><code>@NodeEntity
  @Data
  @NoArgsConstructor
  public class Part {
  
  @Id
  private String name;
  
  public Part(String name) {
      this.name = name;
  }
  }
  </code></pre>
  
  <p>I am able to set <code>parts</code> for a given <code>vehicle</code> and store the nodes in the Neo4j database using a spring data neo4j repository interface.</p>
  
  <pre><code>    Vehicle myVehicle = new Vehicle();
  
      Part brake = new Part("brake");
      Part accelerator= new Part("accelerator");
      Part clutch= new Part("clutch");
  
      myVehicle.setParts(Arrays.asList(brake, accelerator, clutch));
      Vehicle vehicle = vehicleRepository.save(myVehicle);
      System.out.println("vehicle = " + vehicle);
  </code></pre>
  
  <p>I am able to see both the <code>Vehicle</code> and <code>Part</code> nodes in the database using Neo4j browser, something as shown below :</p>
  
  <p><a href="https://i.stack.imgur.com/BlOp4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BlOp4.png" alt="enter image description here"></a></p>
  
  <p>The problem that I am facing is during updating of the existing vehicle.
  I am trying to change the <code>parts</code> that the <code>vehicle</code> has. I am doing as follows:</p>
  
  <pre><code>    Vehicle myVehicle = vehicleRepository.findById(582L).get();
  
      Part brake = new Part("brake");
      Part accelerator= new Part("accelerator");
  
      myVehicle.setParts(Arrays.asList(brake, accelerator));
      Vehicle savedVehicle = vehicleRepository.save(myVehicle);
      System.out.println("vehicle = " + savedVehicle);
  </code></pre>
  
  <p>In the IDE debug session, I can see that the <code>savedVehicle</code>  object (returned after calling <code>save</code> on <code>vehicleRepository</code>) has only 2 parts "brake" and "accelerator". It doesn't have "clutch" part in it, the way I want it to be.</p>
  
  <p>However, when I check the same vehicle object in the Neo4j database using browser, I see that the vehicle object still has 3 parts (even the "clutch").</p>
  
  <p>I am not able to understand why is <code>clutch</code> node still related to the <code>vehicle</code> node as I have overwritten the node and its relationship. Also why is the <code>savedVehicle</code> object different from one in the database.</p>
  
  <p>Is someone able to shed some light on this? Also how can I remove the <code>HAS_PART</code> relationship with <code>clutch</code> node on save/update.</p>
  
  <p>Regards,
  V</p>