title: Problems with cascading deletion in Hibernate
body: <p>This question has been asked numerous times before, yet I haven't seen a satisfactory answer since, thus I am asking it again.</p>
  <h2>Imagine the following situation:</h2>
  <pre><code>public class User {
      ...
  
      @Cascade(value= {CascadeType.DELETE})
      @OneToMany(fetch = FetchType.LAZY)
      @JoinColumn(name=&quot;followerId&quot;)
      public List&lt;LocationFollower&gt; followedLocations;
  
      ...
  }
  
  public class LocationFollower {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = &quot;id&quot;, nullable = false)
      public Long id;
      
      @ManyToOne
      @JoinColumn(name=&quot;locationId&quot;)
      public Location followedLocation;
      
      @ManyToOne
      @JoinColumn(name=&quot;followerId&quot;)
      public User follower;
      
      @Column(name = &quot;followerSince&quot;)
      public Timestamp followerSince;
  }
  
  public class Location {
      ...
  
      @Cascade(value = {CascadeType.DELETE})
      @OneToMany(fetch= FetchType.LAZY)
      @JoinColumn(name=&quot;locationId&quot;)
      public List&lt;LocationFollower&gt; followers;
  
      ...
  }
  </code></pre>
  <p>All I wanted was to delete a user. Logically, one will assume all related &quot;follower&quot;  entries connecting User and Location will be deleted. The same assumption should stay in power if I delete a Location entry.</p>
  <p>What happens actually is that Hibernate tries to update (?!?) the table that holds the followers, and since the related entity (User or Location) has been sent for deletion, tries to set a foreign key, for instance, followed with null. This throws an exception and burst all the succeeding operations.</p>
  <p>The error I get:
  <strong>There was an unhandled failure on the server. Could not execute JDBC batch update; SQL [update locationstofollowers set followerId=null where followerId=?]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update</strong></p>
  <p>P.S. I hear that there was another cascade option DELETE_ORPHAN. This seems to be deprecated, and even though I tried it as well, the effect was the same.</p>