简体   繁体   中英

How to remove Parent and Child Element in JPA/hibernate inheritance strategy Joined?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM