NHibernate Collection Mapping - NHibernate

What is NHibernate Collection Mapping?

In this section, we will be discussing how to signify collections. There are different kinds of collections that we can use within the NHibernate such as −

  • Lists
  • Sets
  • Bags

Currently, from the .NET viewpoint, we usually deal with lists or like very simple data structures, lists, dictionaries. .NET does not have a extensive variety of different collection types. So why does NHibernate need all these different types? It really comes back to the database.

List

  • A list is an ordered collection of elements that are not essentially unique.
  • We can map this using the IList <T>.
  • So although we might conventionally have a list of addresses, and from application point of view we know that the elements are unique, nothing in the list prevents us from inserting duplicate elements in that list.

Set

  • A set is an unordered group of unique elements. If you try to inset 2 replica elements into a set, it will throw an exception.
  • There's not anything specific in NHibernate about it.
  • It's fair a suitable way a have a general set application. If you're on .NET 4, you can use the new HashSet <T> to represent these, but in most NHibernate applications, we represent this is an ISet.
  • It is an unordered, if you pull back a list of addresses from a database or a list of orders, you don't know what order they're coming in unless you put in a specific Order by clause.
  • So in overall, the data that you're pulling back from a database are sets.
  • They are unique collections of elements that are unordered.

Bag

  • One more common collection that we will see in the database world is a bag, which is just like a set but it can have duplicate elements.
  • In the .NET world, we represent this by an IList.

Sets are perhaps the most common, but you will see lists and bags as well depending on your application. Let’s have a look into a below customer.hbm.xml file from the last chapter in which Set orders are defined.

As you can see, we have mapped the orders collection as a set. Remember that a set is an unordered collection of unique elements.
Now, if you look at the Customer class, you will see that Orders property is defined with an ISet as shown in the following program.

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

If the items in the group didn't need to be single, if you could have multiple orders with the same primary key occurring multiple times in this collection, then this would be better mapped as a bag as shown in the following program.

Currently, if you run this application you will get an exception because if we take a look at the customer class, you'll notice that the orders are marked as an ISet in the C# code.

So we will also need to change this to an IList and then here, we would need to change from the HashSet to a List in the constructor.

When you run the application, you will see the same behavior. But, now we can have an order occurring multiple times in the same collection.

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

NHibernate Topics