NHibernate Relationships - NHibernate

What is NHibernate Relationships?

In this section, we will come across relationships in NHibernate. Let's chance our care to how we can understand relationships in NHibernate. The coolest way is to think around the relationships from the database viewpoint.

  • We will first make a new application in which we will create some relationships amongst the client and order entities.
  • The principal relationship we're going to look at is a classic collection relationship.
  • We have a customer with a collection of orders.
  • This is a one-to-many relationship and it's signified in the database by 2 tables and there is a customer ID on the orders table and we have a foreign key relationship back to the customer.

First we need to create a database and two tables Customer and Order. You can create this by specifying the following query in SQL Server Explorer.

It will create two tables in the database. The following image shows the Customer Table.

customer_table

The following image shows the Order Table in which you can see the foreign key relationship back to the customer.

foreign_key_relationship

To install the NHibernate in your application, run the following command in NuGet Manager Console window.

To configure the NHibernate configuration, we need to define the configuration in hibernate.cfg.xml file as shown in the following code.

In this instance, we will be working two domain classes, Customer and Order.

Here is the Customer.cs file implementation in which we have two classes, one is the Customer class and another is the Location class in which object is used as an address in the Customer class.

Here is the mapping file Customer.hbm.xml in which Customer class is mapped to the Customer table.


We also have an Order Class and here is the implementation of Order.cs file.

Many-to-One Relationship

We also need to map the Order class to the Order table in the database, so here is the implementation of the Order.hbm.xml file.


<?xml version = "1.0" encoding = "utf-8" ?>
<hibernate-mapping xmlns = "urn:nhibernate-mapping-2.2" assembly = "NHibernateDemo"
namespace = "NHibernateDemo">

<class name = "Order" table = "`Order`">

<id name = "Id">
<generator class = "guid.comb"/>
</id>

<property name = "Ordered"/>
<property name = "Shipped"/>

<component name = "ShipTo">
<property name = "Street"/>
<property name = "City"/>
<property name = "Province"/>
<property name = "Country"/>
</component>

<!--<many-to-one name = "Customer" column = "CustomerId" cascade =
"save-update"/>-->

</class>
</hibernate-mapping>

One-to-Many Relationship

At this time, we are going to take a look at a one-to-many relationship, in this circumstance, between customer and orders. We've got our customer here, we're creating a new one, and you can see that the collection is initialized with the following pair of orders.

So we will make a new customer and then save it, after saving it, we will discover the ID and then refill it in another session in the Main method as shown in the following program.

Here is the complete Program.cs file implementation.

When you run this application, you will see the resulting output.

As you can see that originally the customer has 2 orders, but when we reload it, there are no orders to be seen. If you look at customer.hbm.xml file, you can see here that we do not map actual orders collection. So NHibernate knows nothing about it. Let's go ahead and add it.


<?xml version = "1.0" encoding = "utf-8" ?>
<hibernate-mapping xmlns = "urn:nhibernate-mapping-2.2"
assembly = "NHibernateDemo" namespace = "NHibernateDemo">

<class name = "Customer">

<id name = "Id">
<generator class = "guid.comb"/>
</id>

<property name = "FirstName"/>
<property name = "LastName"/>
<property name = "AverageRating"/>
<property name = "Points"/>
<property name = "HasGoldStatus"/>
<property name = "MemberSince" type = "UtcDateTime"/>
<property name = "CreditRating" type = "CustomerCreditRatingType"/>

<component name = "Address">
<property name = "Street"/>
<property name = "City"/>
<property name = "Province"/>
<property name = "Country"/>
</component>

<set name = "Orders" table = "`Order`">
<key column = "CustomerId"/>
<one-to-many class = "Order"/>
</set>

</class>
</hibernate-mapping>

This is a set and the name of this collection is ‘Orders’, which is kept in a table called order. We must specify a key which is the name of the foreign key or to find orders. These orders are recognized or belong to a customer through the customer ID. And then we have to note that this is a one-to-many relationship and it is with the order class.
We also need to slightly change the Main method by saving the new customer orders to the database as well as shown in the following program.

We must also state which customer ordered that specific product. So we need to make a many-to-one relationship to tell that order back to that customer.
So let's go into the Order.hbm.xml file and add a many-to-one, and then name the customer field and the column with the customer ID.


<?xml version = "1.0" encoding = "utf-8" ?>
<hibernate-mapping xmlns = "urn:nhibernate-mapping-2.2" assembly = "NHibernateDemo"
namespace = "NHibernateDemo">

<class name = "Order" table = "`Order`">

<id name = "Id">
<generator class = "guid.comb"/>
</id>

<property name = "Ordered"/>
<property name = "Shipped"/>

<component name = "ShipTo">
<property name = "Street"/>
<property name = "City"/>
<property name = "Province"/>
<property name = "Country"/>
</component>

<many-to-one name = "Customer" column = "CustomerId"/>

</class>
</hibernate-mapping>


Let’s run this application again and now you will see the following output.

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

NHibernate Topics