title: JPA Inheritance - parent is not deleted with child
body: <p>can you help with my deleting records with inheritance. I am using Entity manager for objects, and when I call em.remove(childInstance), is removed only child, and parent stay undeleted. Can you tell what I do wrong?</p>
  
  <p>General table:</p>
  
  <pre><code>@MappedSuperclass
  public abstract class General {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "ID", columnDefinition = "serial")
      private Long id;
  
      // some other columns, getters and setters
  }
  </code></pre>
  
  <p>Parent table:</p>
  
  <pre><code>@Entity
  @Table(name = "PARENT_TABLE")
  @Inheritance(strategy = InheritanceType.JOINED)
  public abstract class Parent extends General {
  
      // some columns and getters and setters
  }
  </code></pre>
  
  <p>Child class:</p>
  
  <pre><code>@Entity
  @Table(name = "CHILD_TABLE")
  @OnDelete(action = OnDeleteAction.CASCADE)
  public class Child extends Parent {
  
  @ManyToOne(fetch = FetchType.LAZY)
  @OnDelete(action = OnDeleteAction.CASCADE)
  @JoinColumn(name = "main_child",
          nullable = true,
          columnDefinition = "integer default 0")
  private Child mainChild;
  
  @OnDelete(action = OnDeleteAction.CASCADE)
  @OneToMany(fetch = FetchType.LAZY, mappedBy = "child")
  private Set&lt;MyObjectA&gt; objects = new HashSet&lt;&gt;();
  
      @OnDelete(action = OnDeleteAction.CASCADE)
      @OneToMany(fetch = FetchType.LAZY, mappedBy = "id")
      private Set&lt;Child&gt; subChildren = new HashSet&lt;&gt;();
  
      // some columns and getters and setters
  }
  </code></pre>
  
  <p>MyObjectA class:</p>
  
  <pre><code>@Entity
  @Table(name = "object_a")
  public class MyObjectA extends General {
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "child_id", nullable = false)
      private Child child;
  }
  </code></pre>
  
  <p><strong>EDIT: 1</strong>
  I forgot write that other entities refer to child object, and child object refer to itself.</p>
  
  <p><strong>EDIT: 2</strong>
  I tried add to One To Many this:</p>
  
  <pre><code>cascade = CascadeType.ALL, orphanRemoval = true
  </code></pre>
  
  <p>and it return me that error</p>
  
  <blockquote>
    <p>org.postgresql.util.PSQLException: ERROR: update or delete on table
    "parent" violates foreign key constraint
    "xxxxxxxxxxxxxxxxxxxxxxx" on table "child"</p>
  </blockquote>
  
  <p>I look in database what that constraint is and it refer to that script:</p>
  
  <pre><code>ALTER TABLE public.child
    ADD CONSTRAINT xxxxxxxxxxxxxxxxxxxxxxx FOREIGN KEY (id)
        REFERENCES public.parent (id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION; -- here should be ON DELETE CASCADE
  </code></pre>