Sleeping cascade versus manual removal

I use Hibernate, and several times I had to perform a cascading DELETE operation from the parent to its children. I used the following two options.

One option is to set getChildren () on the parent, add the child to the returned collection of children, and let Hibernate automatically cascade DELETE. The disadvantage of this option is that the getChildren () collection should be open, although it is used only to support Hibernate cascading.

Another option is to manually search for and delete children in ParentDao.delete (parent). The disadvantage of this option is the more customizable code. However, this option may work better if it uses batch deletion operators.

Which approach do you mainly use? Do you see other pros and cons?

+3
source share
1 answer

Which approach do you mainly use? Do you see other pros and cons?

I use cascading when I have a real composition relation (and you want to delete a relatively small number of records). However, I would not introduce such a relation only to implement the deletion, but would use a query (an HQL DELETE array or a native SQL query). In my experience, the benefits outweigh the "cost" of the required additional code (which is not enough anyway).

+2
source

Source: https://habr.com/ru/post/1766551/


All Articles