title: How to remove Parent and Child Element in JPA/hibernate inheritance strategy Joined?
body: <p>I have a entity Photo mapped in JPA/Hibernate that have three different children (PhotoSpot, PhotoSecteur, PhotoVoie). I used @Inheritance(strategy = InheritanceType.JOINED), everything works fine for the persist methods, but I can't manage to make it work when I want to delete one of the photo.</p>
  
  <p>I'm trying to delete a photo just by deleting an element from the parent class, but i don't know if it's possible.
  I looked all the other posts about the same topic, and I think the problem may come from the fact that my child entity has a @ManyToOne asssociation. But I put that association in cascade = CascadeType.ALL on the one side.</p>
  
  <p>Here are my entities (I just put the parent and one child), they are all the same:
  Here is the parent</p>
  
  <pre><code>@Entity
  @Inheritance(strategy = InheritanceType.JOINED)
  public class Photo implements Serializable{
      // todo fichier properties
      private static final String CHEMIN = "D:\\fichiers\\";
      private static final int TAILLE_TAMPON = 10240;
      public static final String CHAMP_PHOTO = "photo";
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      protected Long id;
      protected String nom;
  </code></pre>
  
  <p>Here is one of the child</p>
  
  <pre><code>@Entity
  @Table
  @PrimaryKeyJoinColumn(name = "id")
  @OnDelete(action = OnDeleteAction.CASCADE)
  public class PhotoSpot extends Photo {
  
      @ManyToOne
      private Spot spot;
  </code></pre>
  
  <p>My deleting method :</p>
  
  <pre><code>    public boolean deletePhoto(Long id){
          EntityTransaction transaction = entityManager.getTransaction();
          try {
              transaction.begin();
              Photo photo = entityManager.find(Photo.class, id);
              entityManager.remove(photo);
              entityManager.flush();
              Path path = Paths.get(Photo.getCHEMIN()+photo.getNom());
              Files.delete(path);
              transaction.commit();
              return true;
          }catch (Exception e){
              if (transaction != null)
                  transaction.rollback();
              e.printStackTrace();
              return false;
          }
      }
  </code></pre>
  
  <p>Does the problem happens because I'm doing it the wrong way, and that I should delete first the child element? Normally the cascade option should allow me to delete the child when I delete the parent. Thanks!</p>
  
  <p><strong>EDIT :</strong>
  Ok as i expected, the problem come from the relations. If I enable the org.hibernate.event.internal.DefaultPersistEventListener im getting the <code>un-scheduling entity deletion</code> like a lot of other people. I checked this <a href="https://stackoverflow.com/questions/16898085/jpa-hibernate-remove-entity-sometimes-not-working/16901857#16901857">JPA/Hibernate remove entity sometimes not working</a> this <a href="https://stackoverflow.com/questions/26349213/hibernate-entitymanager-remove-referenced-entity-not-working">Hibernate EntityManager: remove referenced entity not working</a> and this <a href="https://stackoverflow.com/questions/34004855/prevent-hibernate-from-deleting-orphaned-entities-while-merging-an-entity-having/34240940#34240940">Prevent Hibernate from deleting orphaned entities while merging an entity having entity associations with orphanRemoval set to true</a>, but idon't understand exactly what it implies to do in my heritage case. I tried to remove manually the association, but it is still not working. </p>