NHibernate Cascades - NHibernate

What is NHibernate Cascades?

In this section, we will be discussing how to use the Cascade feature. If you have a set or a collection of items or a association amongst two classes such as our client and order and have a foreign key relationship. If we delete the customer by default, NHibernate doesn't do anything to the child objects, so the ones that belong to that customer and we could be orphaning orders.

  • We might also be violating foreign key constraints, so we can use the notion of cascades.
  • By default, NHibernate does not cascade operations to child objects.
  • The aim for this is that you can have relationships such as a customer having a default shipping address and that shipping address is shared with many different customers.
  • So you wouldn't want to cascade that relationship essentially because other customers are still referring to it.
  • So the whole notion of cascades is to tell NHibernate how to handle its child entities.

There are different options for cascading, which are as follows −

  • none − which is the default and it means no cascading.
  • all − which is going to cascade saves, updates, and deletes.
  • save-update − it will cascade, saves and updates.
  • delete − it will cascade deletes.
  • all-delete-orphan − it is a special one which is quite frequently used and is the same as All Except, if it finds Delete-orphan rows, it will delete those as well.

You can state the default in your hbm.xml file, so you can afford a default cascade on that Hibernate mapping component or you can also state it for specific collections and relationships such as the many-to-one.

Let’s have a look into humble instance cascades, let's fix the problem in the program, where we have to manually cascade the save to the orders as shown in the following code.

In the overhead code snippet, you can see that we are manually saving all the orders for the customer. Now let’s remove manual cascade code in which all the orders are saved.

We need to specify the cascade option in customer.hbm.xml.

Currently, orders completely belong to the customer. So if the customers were removed from the database, our application here would want to delete all of those orders, including any that might have been orphaned.

It will end up doing a delete. By that, it will say delete from order table, where the customer ID equals the customer that you're deleting.
So you can actually cascade these deletes. So with the All, it will do saves, updates, and deletes.

Now when you run this application, you will see the following output.

As you can realize that we have removed the code from the program that physically cascaded and our application is still working.
So liable on your relationship, you might want to cascade those. Currently, let's take a look at a different cascade relationship. Let’s go to the Order.hbm.xml file and we can cascade that many-to-one relationship.

So if we make a new order and there's a new customer involved to it and we say, save that order, we might want to cascade that. But one thing that we'd perhaps don't want to do is if an order is deleted to delete the corresponding customer.

So now, we would want to do a save update, so using a save-update, it will cascade any saves or updates to that customer. So, if we get a new customer or if we are changing the customer, it will cascade that. If it is a delete, it won't delete that from the database.

So running our application again, everything still works as expected.

Currently you must have a look at your application, recall that the default is None and you have to think about your entities and your associations between them to control the appropriate cascades for each of your entities as well as each of your relationships in that database.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

NHibernate Topics