NHibernate Mapping Component - NHibernate

What is NHibernate Mapping Component?

In this section, we will be speaking about mapping components. In NHibernate, component is a value object. It does not have an identity of its own.

  • An instance of this would be a money object, a purse or a wallet might have money in it, but the particular identity of that money is irrelevant.
  • It doesn't have its own primary key, but works themselves are determined in the same table as the owning object.

Let’s have a look at a simple instance in which a student has an Address, which is an object of Location class associated with it.

Currently, we also want to update the database by performing the resulting query, which will first drop the Student table and then create a new table that will also contain a column for Location class.

Currently to map those columns that are not straight a part of Student class, but they are properties of Location class and Location class object is defined in the student class. We need a component to map it correctly. Let’s create a component in student.hbm.xml file as shown in the following code.

This component is the Address and it has these dissimilar properties on it. With this info, NHibernate now has sufficient that it can really map this.
Currently here is the Program.cs file in which a new student object is created and initialized and then saved to the database. It will then retrieve the list from the database.

Currently we can execute this application and NHibernate can save those values to the database. When you run the application, you will see the following output.

Here are the values in the database.
values_database

The mechanisms allow us to separate out columns that are in a database table into their own separate class.

  • The extra thing to notice here is since the Location is a class, it is not an object.
  • It is a value type object and it doesn't have its own primary key.
  • It is saved in the same table as the Student that contains it.
  • That's why we're using the component here.
  • This lets a lot of suppleness to change our class layer, how our classes are defined versus how our database is laid out.

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

NHibernate Topics