NHibernate Part II

Introduction:

I know you all must be really anxious to find out that how NHibernate works and how it can make your life easier.

In this article we will look at the add feature of the NHibernate. I will go step by step so you understand what is going on.

Making a Class Library:

The first task is to create a class library which will be mapped using the NHibernate. So, lets make a simple class User.

public class User

{

private int personID;

private string email;

private string password;

private bool status;

private DateTime dateCreated;

// Defining properties

public int PersonID

{

get { return personID; }

set { personID = value; }

}

public string Email

{

get { return email; }

set { email = value; }

}

public string Password

{

get { return password; }

set { password = value; }

}

public bool Status

{

get { return status; }

set { status = value; }

}

public DateTime DateCreated

{

get { return dateCreated; }

set { dateCreated = value; }

}

 

As you can see that there is nothing special about the class. It has some private variables which you can access using the properties. All of this has been explained in my articles before.

NOTE:

   All these properties will map to the database fields. That's why I have created the database script file for you. Just run the script file using query analyzer and it will create table for you. If it does not work than you can easily create a table based on the properties above. This file is available along with the Attachment Links below.

Creating Mapping File:

Now let's take a look at the mapping file for the User.cs class. This mapping file is called (User.hbm.xml). It's a good idea to name the mapping file based on the class name.

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

<class name="Glasses.Test.User, Glasses" table="Person">

<id name="PersonID" column="PersonID" type="Int32" length="4" unsaved-value="0">

<generator class="identity" />

</id>

<property name="Email" column= "Email" type="String" length="50"/>

<property name="Password" type="String" length="50"/>

<property name="Status" type="Boolean" length="1"/>

<property name="DateCreated" type="DateTime"/>

</class>

</hibernate-mapping>

Let's take a look at the mapping file now.

  • Glasses.Test.User is the name of the class that I am using. Glasses is the name of the assembly, Test is the name of the folder and finally user is the class.
  • Person is the name of the table
  • identity means that PersonID is the identity (Automatically generated) column in the database and we don't have to supply it.
  • The <property> tags contain name which is the name of the property in the User.cs class.
  • column is the "database" column.

Note:

Make sure that the mapping file is configured as the "Embedded Resource". Right click on the "User.hbm.xml" file and select properties and set build option to "Embedded Resource".



Attachments

Project Files NHibernateII

1 2

Close    To Top
  • Prev Article-Programming:
  • Next Article-Programming:
  • Now: Tutorial for Web and Software Design > Programming > dotnet > Programming Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction