Angel \”Java\” Lopez on Blog

January 17, 2011

NHibernate 3 (Part 1) Simple Mapping

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

Next post

Year and half ago I wrote an example:

First NHibernate 2.x Example

This year, I want to explore the new release: NHibernate 3.0. You can download the current version from:

I get to use in this solution:

I keep the source code at my AjCodeKatas Google code project, under trunk/NHibernate/SimpleMapping. The code doesn’t include the NHibernate libraries, so you must add the references to the solution. I added all libraries from the NHibernate folders Required_Bins and Required_For_LazyLoading\Castle.

If you are lazy, like me, and you want a ready-to-build solution, you can download the complete example with NHibernate required files from my Skydrive:

Note that I created a solution folder Schemas, and add the .xsd files I found at NHibernate distribution in Required_Bins folder.

The SimpleMapping.Domain contains only a class:

    public class Customer
        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
        public virtual string Address { get; set; }
        public virtual string Notes { get; set; }

This class library has no reference to NHibernate. I wrote an app.config file for the console application:

<?xml version="1.0" encoding="utf-8" ?>
    type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
      <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=NHibernate3SimpleMapping;Integrated Security=True</property>
      <property name="proxyfactory.factory_class">
        NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
      <mapping assembly="SimpleMapping.Console" />

I wrote a Customer.hbm.xml file and mark it as Embedded Resources in his properties window (don’t forget this step: this is the way the example uses to tell NHibernate what classes to map):

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
  <class name="Customer" table="Customers">
    <id name="Id">
      <generator class="guid.comb" />
    <property name="Name" not-null="true" />
    <property name="Address" />
    <property name="Notes" />

To create the database, run Sql\ExecuteAll.cmd (see Readme.txt for more details).

The console program only creates a customer, save it in the database, and retrieve all existing customer:

    ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
    using (ISession session = sessionFactory.OpenSession())
        using (ITransaction tx = session.BeginTransaction())
            Customer customer1 = new Customer();
            customer1.Name = "Customer 1";
            customer1.Address = "Address 1";
            customer1.Notes = "Notes 1";
            IQuery query = session.CreateQuery("from Customer");
            foreach (Customer c in query.List<Customer>())
                System.Console.WriteLine(string.Format("Customer {0}", c.Name));

(Sorry, I’m not very creative in the values of the properties 😉

There are many details to discuss: Why Castle bytecode? what does each property in configuration mean? What is a session? An  a transaction? What is an Id? What is a guid.comb? Options instead of guid.comb? How to map other kind of properties (currency values? dates?).

And what if we need more classes? Inheritance mapping? One to many? Lot of stuff to explore in upcoming posts.

Meanwhile, some resources:

Online NHibernate Documentation

NHibernate available resources

SourceForge project

Book: Jason Dentler’s NHibernate 3.0 Cookbook

Keep tuned!

Angel “Java” Lopez


  1. […] the previous post of this series, I implemented a simple mapping: one class, one […]

    Pingback by NHibernate 3 (Part 2) Table per Hierarchy « Angel “Java” Lopez on Blog — April 5, 2011 @ 9:18 am

  2. […] or download my first example, that includes them. Check my first post to review: libraries to add, schemas, configuration file, and mapping files (don’t forget to […]

    Pingback by NHibernate 3 (Part 3) Table Per Concrete Class « Angel “Java” Lopez on Blog — April 11, 2011 @ 9:50 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

Blog at

%d bloggers like this: