- NHibernate - Fluent Hibernate
- NHibernate - Native Sql
- NHibernate - QueryOver Queries
- NHibernate - Criteria Queries
- NHibernate - Query Language
- NHibernate - Linq
- NHibernate - Load/Get
- NHibernate - Inverse Relationships
- NHibernate - Lazy Loading
- NHibernate - Cascades
- NHibernate - Collection Mapping
- NHibernate - Relationships
- NHibernate - Mapping Component
- NHibernate - Caching
- NHibernate - Batch Size
- NHibernate - Override Configuration
- NHibernate - Configuration
- NHibernate - Data Types Mapping
- Add Intelliesnse To Mapping File
- NHibernate - Profiler
- NHibernate - Basic Crud Operations
- NHibernate - Basic Orm
- NHibernate - Getting Started
- NHibernate - Environment Setup
- NHibernate - Orm
- NHibernate - Architecture
- NHibernate - Overview
- NHibernate - Home
NHibernate Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
NHibernate - Mapping Component
In this chapter, we will be talking about mapping components. In NHibernate, component is a value object. It does not have an identity of its own.
An example of this would be a money object, a purse or a wallet might have money in it, but the exact identity of that money is irrelevant.
It doesn t have its own primary key, but components themselves are persistent in the same table as the owning object.
Let’s have a look at a simple example in which a student has an Address, which is an object of Location class associated with it.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NHibernateDemoApp { class Student { pubpc virtual int ID { get; set; } pubpc virtual string LastName { get; set; } pubpc virtual string FirstName { get; set; } pubpc virtual StudentAcademicStanding AcademicStanding { get; set; } pubpc virtual Location Address { get; set; } } pubpc class Location { pubpc virtual string Street { get; set; } pubpc virtual string City { get; set; } pubpc virtual string Province { get; set; } pubpc virtual string Country { get; set; } } pubpc enum StudentAcademicStanding { Excellent, Good, Fair, Poor, Terrible } }
Now, we also need to update the database by executing the following query, which will first drop the Student table and then create a new table that will also contain a column for Location class.
DROP TABLE [dbo].[Student] CREATE TABLE [dbo].[Student] ( [ID] INT IDENTITY (1, 1) NOT NULL, [LastName] NVARCHAR (MAX) NULL, [FirstMidName] NVARCHAR (MAX) NULL, [AcademicStanding] NCHAR(10) NULL, [Street] NVARCHAR (100) NULL, [City] NVARCHAR (100) NULL, [Province] NVARCHAR (100) NULL, [Country] NVARCHAR (100) NULL, CONSTRAINT [PK_dbo.Student] PRIMARY KEY CLUSTERED ([ID] ASC) );
Now to map those columns that are not directly 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.
<?xml version = "1.0" encoding = "utf-8" ?> <hibernate-mapping xmlns = "urn:nhibernate-mapping-2.2" assembly = "NHibernateDemoApp" namespace = "NHibernateDemoApp"> <class name = "Student"> <id name = "ID"> <generator class = "native"/> </id> <property name = "LastName"/> <property name = "FirstName" column = "FirstMidName" type = "String"/> <property name = "AcademicStanding"/> <component name = "Address"> <property name = "Street"/> <property name = "City"/> <property name = "Province"/> <property name = "Country"/> </component> </class> </hibernate-mapping>
This component is the Address and it has these different properties on it. With this information, NHibernate now has enough that it can actually map this.
Now here is the Program.cs file in which a new student object is created and initiapzed and then saved to the database. It will then retrieve the pst from the database.
using HibernatingRhinos.Profiler.Appender.NHibernate; using NHibernate.Cache; using NHibernate.Caches.SysCache; using NHibernate.Cfg; using NHibernate.Dialect; using NHibernate.Driver; using NHibernate.Linq; using System; using System.Linq; using System.Reflection; namespace NHibernateDemoApp { class Program { static void Main(string[] args) { NHibernateProfiler.Initiapze(); var cfg = new Configuration(); String Data Source = asia13797\sqlexpress; String Initial Catalog = NHibernateDemoDB; String Integrated Security = True; String Connect Timeout = 15; String Encrypt = False; String TrustServerCertificate = False; String ApppcationIntent = ReadWrite; String MultiSubnetFailover = False; cfg.DataBaseIntegration(x = > { x.ConnectionString = "Data Source + Initial Catalog + Integrated Security + Connect Timeout + Encrypt + TrustServerCertificate + ApppcationIntent + MultiSubnetFailover"; x.Driver<SqlCpentDriver>(); x.Dialect<MsSql2008Dialect>(); }); cfg.AddAssembly(Assembly.GetExecutingAssembly()); var sefact = cfg.BuildSessionFactory(); using (var session = sefact.OpenSession()) { using (var tx = session.BeginTransaction()) { var student1 = new Student { ID = 1, FirstName = "Allan", LastName = "Bommer", AcademicStanding = StudentAcademicStanding.Poor, Address = new Location { Street = "123 Street", City = "Lahore", Province = "Punjab", Country = "Pakistan" } }; session.Save(student1); tx.Commit(); var students = session.Query<Student>().ToList<Student>(); Console.WriteLine(" Fetch the complete pst again "); foreach (var student in students) { Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7}", student.ID, student.FirstName, student.LastName, student.AcademicStanding, student.Address.Street, student.Address.City, student.Address.Province, student.Address.Country ); } } Console.ReadLine(); } } } }
Now we can run this apppcation and NHibernate can save those values to the database. When you run the apppcation, you will see the following output.
Fetch the complete pst again 2 Allan Bommer Poor 123 Street Lahore Punjab Pakistan
Here are the values in the database.
The components allow us to separate out columns that are in a database table into their own separate class.
The other thing to notice here is because the Location is a class, it is not an entity.
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 allows a lot of flexibipty to change our class layer, how our classes are defined versus how our database is laid out.