Hibernate handles cascades internally. Executing a delete
query won't trigger the internal cascades, which will result in inconsistencies / orphans. This you might have tried and faced foreign key constraint error.
There are two ways to delete a list of entities along with their child entities:
- Select the list of entities using
selectionCase
. Iterate through the list and delete each one individually usingsession.delete
. - Delete the records manually. Write separate
delete
statements. To avoid the violation of foreign key constraints, you need to delete child records before deleting the parents. This will perform better than the first option.