NHibernate Part II
CoderSource.net
NHibernate Part II - Article by azamsharp
Level: BeginnerType: Article
Rating: Page: 1 of 2

Date: 6/22/2005 12:00:00 AM

Environment: Windows, .Net, NHibernate

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

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us