开发者

Hibernate cascade vs manual delete

开发者 https://www.devze.com 2023-01-18 02:02 出处:网络
I am using H开发者_开发知识库ibernate and a few times had to implement cascading DELETE operation from parent object to its children. I used the following two options.

I am using H开发者_开发知识库ibernate 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?


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消