Okey, so I'm currently trying to figure out how to get cascade deletion working.
Here are my annotated JPA Classes:
Code:
public class Entity {
@OneToMany(mappedBy = "vistedEntity", cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, orphanRemoval = true)
@OnDelete(action = OnDeleteAction.CASCADE)
private Set<Visit> entityVisits;
...
}
public class Visit extends Entity {
@ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@OnDelete(action = OnDeleteAction.CASCADE)
private Entity vistedEntity;
...
}
public class DatabaseWorker {
public void deleteEntity(Entity entity) {
EntityManager em = produceEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
entity = em.merge(entity);
em.remove(entity);
tx.commit();
em.close();
}
...
}
And now I want that Hiberate will successfully delete Entity instances, for which Visits exist. But instead my database throws an Exception because of a constraint violations.
My favorite solution would be for hibernate to just modify the database table and add the "ON DELETE CASCADE", which should have happened by adding the @OnDelete annotation from what I've read, but sadly it doesn't.
My usecase doesn't allow me to do this manually, I need the tables to be created and managed by hibernate.