NHibernate Inverse Relationships - NHibernate

What is NHibernate Inverse Relationships?

  • In this section, we will be covering additional feature which is Inverse Relationships. It is an entertaining option that you will see on collection that are inversely equivalent to true and it also confuses a lot of designers. So let's talk about this option. To understand this, you actually need to think about the relational model. Let’s approximately you have a bidirectional relations using a single foreign key.
  • From a relational standpoint, you must acquire one foreign key, and it signifies both customer to order and orders to customer.
  • From the OO model, you have unidirectional relations using these situations.
  • There is nothing that speaks that two unidirectional associations signify the same bidirectional association in the database.
  • The difficult here is that NHibernate doesn't have sufficient info to know that customer.orders and order.customer represent the same relationship in the database.
  • We need to offer inverse equals true as a hint, it is since the unidirectional relations are using the same data.
  • If we attempt to save these relations that have 2 references to them, NHibernate will attempt to update that reference twice.
  • It will really do an extra roundtrip to the database, and it will also have 2 updates to that foreign key.
  • The inverse equals true tells NHibernate which side of the relationship to ignore.
  • Once you apply it to the collection side and NHibernate will continuously update the foreign key from the other side, from the child object side.
  • Then we only have one update to that foreign key and we don't have additional updates to that data.
  • This lets us to stop these duplicate updates to the foreign key and it also helps us to prevent foreign key violations.

Make sure you have an look at the customer.cs file in which you will see the AddOrdermethod and the idea here is that we now have this back pointer from order back to customer and it wants to be set. So when an order is added to a customer, that customer's back pointer is set, otherwise, it would be null, and so we need this to keep this linked correctly composed in the object graph.

Here is the Program.cs file implementation.

It is going to save that to the database and then reload it. Currently let’s run your request and open the NHibernate Profiler and see how it really saved it.

You will notice that we have 3 sets of statements. The main one will insert the customer, and that customer's ID is the Guid, which is highlighted. The next statement is put in into the orders table.

You will notice the same Customer Id Guid is set in there, so must that foreign key set. The last statement is the update, which will update the foreign key to the same customer id once again.


Currently the difficult is that the customer has the orders, and the orders have the customer, there's no way that we haven't told NHibernate that it's actually the same relationship. The way we do this is with inverse equals true.

So let's go to our customer.hbm.xml mapping file and set the inverse equal to true as shown in the following code.

After saving the orders, it will set that foreign key from the order side. Now let’s run this application again and open the NHibernate profiler.


If we look at how those are inserted, we get the insert in the customer, and the insert into orders, but we don't need that duplicate update of the foreign key because it's being updated when the orders are being saved.

  • Currently, you must note that if you simply have a unidirectional association and it's the set that is keeping this relationship, then if you turn inverse equals true, that foreign key is never going to be set, and those items are never going to have their foreign keys set in the database.
  • If you look at the many-to-one relationship in the Order.hbm.xml file and you look for inverse, it doesn't really have an inverse attribute.
  • It all the time is set from the child item, but if you need a many-to-many collection, you can set it from either side.

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

NHibernate Topics