I am trying to cascade delete rows in a join table via one of its foreign keys and it has another table related to it that I would like to remove all rows associated with this ID as well. So it looks like the diagram below. When I use Session.delete(reqCandObject) with hibernate it works fine and cascades through deleting the One entry from the candidate_jobReq table as well as the associated comments. However, I want to delete all of the candidate_jobReq entries that have a certain candidate ID (and also delete the comments) I tried the function below but unlike the nice hibernate.delete(object) function, this one runs into a foreign key constraint error. How can I delete these rows while having hibernate cascade the delete for me?
public void deleteWhere(String selectionCase){
Session hibernateSession = this.getSession();
try {
hibernateSession.beginTransaction();
Query q = hibernateSession.createQuery("delete "+ type.getSimpleName() +" where " + selectionCase);
q.executeUpdate();
hibernateSession.getTransaction().commit();
} finally {
hibernateSession.close();
}
}