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.
There are different options for cascading, which are as follows −
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.
NHibernate Related Interview Questions
|SQL Server 2008 Interview Questions||ASP.NET Interview Questions|
|LINQ Interview Questions||IBM Tivoli Interview Questions|
|Windows Presentation Foundation(WPF) Interview Questions||Windows Communication Foundation (WCF) Interview Questions|
|Ibm Tivoli Netcool Omnibus Interview Questions||Dynamic Link Library (DLL) Interview Questions|
|Advanced C# Interview Questions||Asp Dot Net Mvc 4 Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.