I need to delete an entity with its children entities. Here is my code :
public void deleteCascade(Integer entityId) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaDelete<MyEntity> criteriaDelete = cb.createCriteriaDelete(MyEntity.class);
Root<MyEntity> myEntity = criteriaDelete.from(MyEntity.class);
criteriaDelete.where(
cb.equal(MyEntity.get(MyEntity_.id), entityId)
);
em.createQuery(criteriaDelete).executeUpdate();
}
This will obviously not work because of orphans. I know 2 solutions which do not statisfy me :
- Manually delete orphans, one request per entity.
- Anotate the entity attributes.
I would actually prefer using a simple parameter somewhere but cannot find if it exists.
Any idea?