Pergunta

I am using Hibernate and a few times had to implement cascading DELETE operation from parent object to its children. I used the following two options.

One option is to expose getChildren() on the parent object, add the child to the returned collection of children and allow Hibernate to cascade DELETE automatically. The disadvantage of this option is that getChildren() collection needs to be exposed even though it's only used to support Hibernate cascading.

Another option is to look up and delete children manually in ParentDao.delete(parent). The disadvantage of this option is more custom code. However, this option may perform better if it uses batch delete statements.

What approach do you mostly use? Do you see other pros and cons?

Foi útil?

Solução

What approach do you mostly use? Do you see other pros and cons?

I use cascading when I have a real composition relation (and want to delete a relatively low number of records). However, I wouldn't introduce such a relation just to implement a delete, but use a query (bulk HQL DELETE or native SQL query). To my experience, the benefits outweigh the "cost" of the additional code required (which is small anyway).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top