• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is there a case in hibernate when cascading should not be used?

 
Monica Shiralkar
Ranch Hand
Posts: 873
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt regarding the cascading feature of hibernate. From what I have understood, cascading is such a useful feature of hibernate where when we save in one table it also saves in the other table without us to perform the operation on the second table. When it is such a useful feature, shouldnt it be used in all the cases. We have the option to not use cascading but why would someone not use cascading. Is there a case where cascading should not be used?

thanks
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's indeed a very useful and convenient feature.

But this feature come with a price, if you do not use it wisely (when updating and/or deleting), many unnecessary cascade effects will be generated (cascade update) slowing down your performance, or deleting (cascade delete) some data you didn’t expected.
 
Monica Shiralkar
Ranch Hand
Posts: 873
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this feature come with a price, if you do not use it wisely (when updating and/or deleting), many unnecessary cascade effects will be generated (cascade update) slowing down your performance, or deleting (cascade delete) some data you didn’t expected.


Suppose there are tables Categories, products. If I do save operation on Categories , if cascading is not used another save operation would be required on Products which will not be automatic but will have to be specified. So what was the use of not using cascading and then specifying save operation on products too?

thanks
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica Shiralkar wrote:Suppose there are tables Categories, products. If I do save operation on Categories , if cascading is not used another save operation would be required on Products which will not be automatic but will have to be specified. So what was the use of not using cascading and then specifying save operation on products too?

If it makes sense to have a cascade effect when saving a Category, then just do it. It's not wrong or a bad practice! I use it myself as well, but it is always a well-informed decision (not something which is done without thought). And I always check/analyze the generated queries by the ORM tool to make sure no unneeded/unwanted/unexpected queries are generated.
 
Monica Shiralkar
Ranch Hand
Posts: 873
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we save Category and it does not automatically save Product (cascading is not used), then we will have to do a manual save operation on Product, so why would someone not automatically save Product and do manual work.

thanks
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica Shiralkar wrote:If we save Category and it does not automatically save Product (cascading is not used), then we will have to do a manual save operation on Product, so why would someone not automatically save Product and do manual work.

For the exact same reasons I already explained twice in previous posts and I won't repeat them again (although third time's a charm )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic