The simple answer is yes. If you want to delete Group , and you know that there are tons of records in the GroupUser table, then it is much better to create a delete request that will do everything in one batch, rather than one and one.
If you do not have cascading in the base database (or even if you do), its good practice to do this in the correct order.
So, first uninstall GroupUser .
Assuming you have a Group object that you want to delete.
int numberDeleted = entityManager.createQuery("DELETE FROM GroupUser gu WHERE gu.group.id=:id").setParameter("id",group.getId()).executeUpdate();
The returned int indicates how many records are deleted.
Now you can finally delete Group
entityManager.remove(group); entityManager.flush();
UPDATE
It seems that @OnDelete on @OneToMany does the trick
Shervin asgari
source share