0

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.

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.

Here are my entities (I just put the parent and one child), they are all the same: Here is the parent

@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;

Here is one of the child

@Entity
@Table
@PrimaryKeyJoinColumn(name = "id")
@OnDelete(action = OnDeleteAction.CASCADE)
public class PhotoSpot extends Photo {

    @ManyToOne
    private Spot spot;

My deleting method :

    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;
        }
    }

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!

EDIT : Ok as i expected, the problem come from the relations. If I enable the org.hibernate.event.internal.DefaultPersistEventListener im getting the un-scheduling entity deletion like a lot of other people. I checked this JPA/Hibernate remove entity sometimes not working this Hibernate EntityManager: remove referenced entity not working and this Prevent Hibernate from deleting orphaned entities while merging an entity having entity associations with orphanRemoval set to true, 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.

Alain Duguine
  • 425
  • 7
  • 21

1 Answers1

0

whenever parent delete then all children automatically deleted

class Product {
    String name;
    @OneToMany(cascade = CascadeType.REMOVE)
    List<AProduct> aProduct;
}

class AProduct {
    String name;
    @ManyToOne(cascade = CascadeType.REMOVE)
    Product product;
}
Andrei Tanana
  • 7,932
  • 1
  • 27
  • 36
  • Thaanks for your answer ! Just to be sure, do you mean i should replace the heritage by simple associations ? – Alain Duguine Sep 02 '19 at 16:38
  • yes and you can also move your code Photo photo = entityManager.find(Photo.class, id);code in service class and why you create transaction by own give all to framework just use the entity manager's methods. – mandy rayat Sep 02 '19 at 16:52
  • Ok thanks, i will try that tomorrow. The transaction ? Im not using spring so i guess i have to do it by hand no ? – Alain Duguine Sep 02 '19 at 16:59
  • I thought about your solution, but i'm not sure it fits my project (or maybe i don't understand it properly).There is no a toMany relations between my table photo and the childs. Its clearly an inheritance scheme for me. All PhotoSpot, PhotoSecteur and PhotoVoie are... Photos. Or i should reverse my relation having the PhotoSecteur, PhotoVoie, PhotoSpot ont the side one and the Entity Photo on the side Many. But normmaly with inheritance i should be able to delete everything properly, wiithout changing my structure. – Alain Duguine Sep 03 '19 at 04:32