Nhibernate Parent Child Relations

For almost every Nhibernate beginner the most challenging thing to do is implement a parent child or one to many relation. There are many articles about this on the net some very basic and some highly detailed. But most samples available do not run when you download them giving all sorts of Nhibernate errors, which well obviously make no sense to someone who is using Nhibernate for the first time.

So this is my attempt at building a small Nhibernate one to many sample that will hopefully run without any issues and help in implementing basic relations using Nhibernate.

The Database Structure

The database structure I have chosen is a one to many relation between Department and User, where one department can have multiple users. The entity relation diagram is as shown below.

NHSample_dbStructure

Building the ORM

We will be building a new class library which may be considered as a data layer using Nhibernate. The object relation mapping for Nhibernate needs to be defined as shown below.

NHSample_ClassDiagram

The class structure is exactly similar to the database entity diagram shown above. Each property in the class maps to a column in the database. However you may notice that there is no Department Id column in User class. Instead the foreign key relation is established by the user list defined in Department class. This collection is similar to a foreign key relation in the database.

The next step is to define the Database to Class mapping in XML files. For the user class create a new XML file and name it User.hbm.xml. The .hbm is required for Nhibernate to compile it.

Show Code

Similarly for the department class define an Department.hbm.xml file as shown below.

Show Code

For the collection we use a tag called bag which defines relation between the two classes.

Now one at a time right click the hbm files and in their properties set the Build Action as Embedded Resource. This will embed the xml files in the dll.

Building A Session Factory

For Nhibernate to connect to a database we need to declare a Session Factory which will build an Nhibernate session.

Show Code

Performing Operations

Create a new session object for performing operations on database. For example to create a new department declare a department object and save it using ISession.Save(object).

Show Code

Refer the attached sample for more operations.

Running the Sample

The sample contains a SQL 2008 database mdf file which contains the table structure required to test the sample. Open the App.config file in TestProject and modify the connection string and give the exact location of that file on your file system. Also provide the user name password for your local sql server instance.

Show Code

If you have an instance of SQL server 2005 installed, modify the dialect tag as shown below.

Show Code

Download Sample
NHibernateSample.zip

    • feel
    • January 16th, 2010 7:14pm

    thanks for information for c# artical i am looking for download

    http://csharptalk.com

    feel

    • Ted
    • January 19th, 2010 10:49pm

    In no way did I manage to open or attach the database file. Attempts with both instances SQL 2005 and 2008 failed. I tried everything on earth…

    FCB::Open failed: Could not open file D:\Samples\NHibernateSample\TestDatabase\TestDatabase.mdf for file number 0. OS error: 5(error not found).

    I also gave all security rights on the SQL service user with no success.

    • Ted
    • January 20th, 2010 6:01pm

    I fixed the problem for me.
    The database is detached from SQL 2008. Though there is COMPATIBILITY_LEVEL = 90 set in, it still “cannot be opened because it is version 655. This server supports version 612 and earlier”.
    I attached it in SQL2008, scripted in sql text file, removed the 2008-specific lines:

    ALTER DATABASE [TestDatabase] SET COMPATIBILITY_LEVEL = 90
    ALTER DATABASE [TestDatabase] SET HONOR_BROKER_PRIORITY OFF

    And ran the create script in SQL 2005. Then I changed the “connection.connection_string” property in app.config to:
    “Server=.\SQLExpress;Database=TestDatabase;…..”
    and it’s finally OK in SQL2005.
    Still cannot figure out why it wouldn’t just open the mdb neither in 2005 nor in 2008…

    • Priye
    • April 2nd, 2010 6:11pm

    Dear Rohan , I am your big fan. But since long u havent updated nething? Y so??? Please do share ur creativity wid us Jst a suggestion.

  1. oh thank, I fixed the problem and found the solution. The first thing I hardly configure the error but in due time I successfully managed it.

  2. I recently download the NHibernateSample.zip and it works!! just simply modify the tag;
    NHibernate.Dialect.MsSql2005Dialect

    and its properly on a right track. :)

  3. though the table structure required to test the sample and easily modify the connection.

  4. This is easy enough when you allow the foreign keys to be null in your database.

  5. NHibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again.

  6. One of the first things that new users want to do with Hibernate is to model a parent/child type relationship. This is the first thing that I’ve learned about nhibernate.

  7. Just encountered errors but then when refreshing,its totally okay now. hehe. it is useful though.

  8. Hibernate will handle all the associations automatically, but we need to customize some POJOs and mapping files.

  9. I finally settled on having the parent as a Category as opposed to a long and got the association working.

  10. It is helpful to me.

  1. January 19th, 2011
    Trackback from : bidcactus – what is it ?
  2. January 30th, 2011
    Trackback from : My Blog

Spam protection by WP Captcha-Free