Angel \”Java\” Lopez on Blog

June 3, 2009

First NHibernate 2.x example

Filed under: .NET, C Sharp, NHibernate — ajlopez @ 9:23 am

I was working creating a .NET solution, containing a minimal NHibernate 2.x example. I’m using NHibernate GA 2.0.1 binary distribution, following the reference manual instruction.

You can download the source code from my Skydrive folder: 

I was using the online documentation from

and I built the same manual in .pdf format, from the SVN trunk:

You can download the manual in .pdf from


The solution contains two projects: a class library, and a console application:

The domain contains only one class, Cat (following the first example included in the NHibernate doc):

namespace NHibernateExample1.Domain
    public class Cat
        private string id;
        private string name;
        private char sex;
        private float weight;

        public Cat()
        public virtual string Id
            get { return id; }
            set { id = value; }

        public virtual string Name
            get { return name; }
            set { name = value; }

        public virtual char Sex
            get { return sex; }
            set { sex = value; }

        public virtual float Weight
            get { return weight; }
            set { weight = value; }

There is an embedded resource describing the mapping: Cat.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHibernateExample1.Domain" assembly="NHibernateExample1.Domain">
  <class name="Cat" table="Cats">
    <!-- A 32 hex character is our surrogate key. It's automatically
generated by NHibernate with the UUID pattern. -->
    <id name="Id">
      <column name="CatId" sql-type="char(32)" not-null="true"/>
      <generator class="uuid.hex" />
    <!-- A cat has to have a name, but it shouldn' be too long. -->
    <property name="Name">
      <column name="Name" length="16" not-null="true" />
    <property name="Sex" />
    <property name="Weight" />

There is an ExecuteAll.cmd in Sql folder, that creates the database (you can pass the MS SQL Server as parameter, .\SQLEXPRESS is the default value):

The console application is simple: configures NHibernate, gets a session, inserts new objects inside a transaction, executes a query, and list the result:

        static void Main(string[] args)
            ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();

            ISession session = sessionFactory.OpenSession();
            ITransaction tx = session.BeginTransaction();

            Cat cat;

            cat = new Cat() { Name = "Moe", Sex = 'M', Weight = 9.0f };


            cat = new Cat() { Name = "Larry", Sex = 'M', Weight = 8.5f };


            cat = new Cat() { Name = "Sue", Sex = 'F', Weight = 7.5f };



            IQuery query = session.CreateQuery("select c from Cat as c where c.Sex = :sex");

            query.SetCharacter("sex", 'M');

            foreach (Cat kitty in query.Enumerable())
                System.Console.Out.WriteLine("Male Cat: " + kitty.Name);



Running the console application inserts this data in the database:

The example is simple. Now I have an updated running example using NHibernate 2.0, I’m planning to update my AjGenesis templates for NHibernate to use that version.

Angel “Java” Lopez


  1. The type initializer for ‘NHibernate.Proxy.CastleProxyFactory’ threw an exception. i need help

    Comment by ramalingam — December 22, 2010 @ 8:14 am

  2. […] First NHibernate 2.x Example […]

    Pingback by NHibernate 3 (Part 1) Simple Mapping « Angel “Java” Lopez on Blog — January 17, 2011 @ 9:27 am

  3. […] First NHibernate 2.x Example […]

    Pingback by NHibernate 3 (Parte 1) Un mapeo simple - Angel "Java" Lopez — January 18, 2011 @ 10:05 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Blog at


Get every new post delivered to your Inbox.

Join 69 other followers

%d bloggers like this: