title: Delete in bidrectional self referenced table leads to IntegrityConstraintViolationException in JPA
body: <p>I'm using JPA in combination with Spring Boot (if it matters) and created two tables FileSystem and Media. Both are connected over an InheritanceType.JOINED relationship (see ER-diagram). Besides, Filesystem is a self-referencing table which stores a "tree" of files/folders with their childs and parents. 
  <img src="https://i.stack.imgur.com/M1krX.png" alt="ER-diagram"></p>
  
  <p>The code is as follows:</p>
  
  <p><strong>FileSystem.java</strong></p>
  
  <pre><code>@Entity
  @Inheritance(strategy=InheritanceType.JOINED)
  public class FileSystem {
  
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column
   private long id;
  
   private String name;
  
   public enum Type { FOLDER, FILE}
  
   @ManyToOne
   @JoinColumn(name="parent")
   @JsonBackReference
   private FileSystem parent;
  
   @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL,orphanRemoval = true)
   @JsonManagedReference
   List&lt;FileSystem&gt; childs = new ArrayList&lt;FileSystem&gt;();
  
   FileSystem.Type type;
   }
  </code></pre>
  
  <p><strong>Media.java</strong></p>
  
  <pre><code>@Entity
  public class Media extends FileSystem implements FieldHandled{
  @JsonIgnore
  private FieldHandler fieldHandler;
  
  @JsonIgnore
    @Lob
    @Column( length = 100000 )
    @Basic(fetch = FetchType.LAZY, optional = false)
   private byte[] data;
   }
  </code></pre>
  
  <p><strong>MediaRepository.java</strong></p>
  
  <pre><code>public interface MediaRepository extends JpaRepository&lt;Media, Long&gt;{
  Media save(Media pageTemplate);
  
  Media findOne(Long id);
  
  public List&lt;Media&gt; findAllByParentId(Long parent);
  public List&lt;Media&gt; findAllByParentIsNull();
  public List&lt;Media&gt; findByName(String name);
  }
  </code></pre>
  
  <p><strong>Deletion in my controller method</strong></p>
  
  <pre><code>...
  MediaRepository mediaRepository;
  mediaRepository.delete(mediaToDelete);
  ...
  </code></pre>
  
  <p>Adding new media's, fetching them etc. works fine so far. But when I call <code>delete()</code> for a <code>FileSystem</code> that has multiple childs (and subchilds) using my extended <code>JpaRepository</code> I get the following exception:</p>
  
  <blockquote>
    <p>Cannot delete or update a parent row: a foreign key constraint fails (<code>repo</code>.<code>file_system</code>, CONSTRAINT <code>FKf57w77uiwcpfmbtl9lpuq9j9m</code> FOREIGN KEY (<code>parent</code>) REFERENCES <code>file_system</code> (<code>id</code>))</p>
  </blockquote>
  
  <p>The strangest thing about this is the log. It seems that in the first step JPA is removing the media row (which IMO is correct since the InheritanceType is JOINED) and then tries to remove the filesystem row (which IMO is not correct because it should first remove all it's childs recursively).:</p>
  
  <pre><code>2016-10-19 16:21:43.530 TRACE 11412 --- [nio-8080-exec-1] org.hibernate.type.EnumType              : Returning [FOLDER] as column [type3_6_0_]
  2016-10-19 16:21:43.530 DEBUG 11412 --- [nio-8080-exec-1] org.hibernate.SQL                        : delete from media where id=?
  Hibernate: delete from media where id=?
  2016-10-19 16:21:43.531 TRACE 11412 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [65]
  2016-10-19 16:21:43.533 DEBUG 11412 --- [nio-8080-exec-1] org.hibernate.SQL                        : delete from file_system where id=?
  Hibernate: delete from file_system where id=?
  2016-10-19 16:21:43.534 TRACE 11412 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [65]
  2016-10-19 16:21:43.535  WARN 11412 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1451, SQLState: 23000
  2016-10-19 16:21:43.535 ERROR 11412 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : Cannot delete or update a parent row: a foreign key constraint fails
  </code></pre>