I have a very simple unidirectional @OneToMany from a Parent object to a List of children with CascadeType.ALL. How would I correctly remove and delete one of the children?
Simply calling remove(child) on the List and then session.saveOrUpdate(parent) of course does not work and the child is not deleted in the database unless I specify orphan removal.
As an alternative to orphan removal, would it be correct if I session.delete(child) to delete it in the DB, then remove(child) from the List and do I then have to session.refresh(parent) so my parent object in memory has the right state?
How would I correctly remove the child and have it deleted in the database without orphan removal?
I am currently thinking about this in my ParentDao:
public void removeChild(Parent parent, Child child) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
session.beginTransaction();
session.delete(child);
session.getTransaction().commit();
parent.getChildren().remove(child);
session.refresh(parent);
} catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}