I am trying to delete a child record when parent is deleted. There is no column in the parent table that refers to child. The child refers to parent in a one-to-one optional relationship.
When parent is being deleted, a constraint is thrown due to the fact relationship still exists. If I add the set relationship to the child side, it does not help. Hibernate does not delete the child record since I am guessing, child record was never fetched.
Is there a way to delete child records short of doing it in an interceptor ? Thanks.
I will provide the example to a similar situation, applying which to your situation will solve the problem.
Lets assume that Employee has a Department_ID in it’s table, and Department does not have any column referencing Employee_ID.
We associate employee with a department using join column. This way we get uni-directional association.
Next we associate department with employee by marking association with
mappedByattribute. It references the owning field of the association on the Employee side. This way we get bi-directional association.Marking association with CascadeType.ALL will include CascadeType.REMOVE, which will cascade to Employee on remove operation. Now, removing department, will remove employee along with it.