title: Spring Data REST - Exception when PATCH is sent with an empty array in request body
body: <p>I am using Spring Data REST and I have the following entities in my domain.</p>
  
  <p><strong>Atoll.class</strong></p>
  
  <pre><code>@Entity
  @Data
  public class Atoll {
  
      @Id
      @GeneratedValue
      private Long id;
  
      private String atollName;
  
      @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
      @JoinColumn(name = "atoll_id")
      private List&lt;Island&gt; islands = new ArrayList&lt;&gt;();
  
  }
  </code></pre>
  
  <p><strong>Island.class</strong></p>
  
  <pre><code>@Entity
  @Getter
  @Setter
  public class Island extends Auditable{
  
      @Id
      @GeneratedValue
      private Long id;
  
      private String islandName;
  
      @Override
      public boolean equals(Object o) {
          if (this == o) {
              return true;
          }
          if (o == null || getClass() != o.getClass()) {
              return false;
          }
          Island that = (Island) o;
          return Objects.equals(this.getCreatedDate(), that.getCreatedDate());
  
      }
  
      @Override
      public int hashCode() {
          return Objects.hash(this.getCreatedDate());
      }
  
  }
  </code></pre>
  
  <p>Now I can persist both Atoll and Islands by sending a POST to /atolls endpoint with the following JSON.</p>
  
  <pre><code>{
      "atollName":"Haa Alif",
      "islands":[{
          "islandName":"Dhidhdhoo"
      }]
  }
  </code></pre>
  
  <p>But when I send a PATCH to <code>atolls/1</code> with an empty array of islands to remove all the islands in the atoll it gives me the following exception</p>
  
  <pre><code>A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: com.boot.demo.model.Atoll.islands; nested exception is org.hibernate.HibernateException: A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: com.boot.demo.model.Atoll.island
  </code></pre>
  
  <p>I searched stackoverflow and found this <a href="https://stackoverflow.com/questions/5587482/hibernate-a-collection-with-cascade-all-delete-orphan-was-no-longer-referenc">answer</a> where it says this mainly occurs due to equals and hashCode method so I override the default equals and hashCode method but the error is still the same(the <code>createdDate</code> used in equals method is part of <code>Auditable</code> class).</p>
  
  <p>So what causes this exception and how do I fix this?</p>