Ian Cooper

Sponsors

The Lounge

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Introduction to NHibernate, Part 1

To go alongside my set of posts on LINQ To SQL I wanted to post an Introduction to NHibernate. These posts are not intended as any kind of masterclass, so experienced NHibernate hands might want to skip them. They are intended for folks who are new to the framework to help get them started.

In this first session we will introduce NHIbernate and the basics of mapping.

Where, who what, when, why?

NHibernate is an open source ORM, derived initially from the Hibernate project. NHibernate supports persistence ignorance, your object classes don't have to follow a restrictive programming model. Persistent classes do not need to implement any interface or inherit from a special base class.Hibernate was developed by a team of Java software developers around the world led by Gavin King.

NHibernate 2.0.1GA is the current version of NHIbernate.

Building NHibernate

You can just download the binaries that you need, especially if you have no appetite for un-released versions, however if you want the latest features then you need to download the latest version of the source and build it.

The NHibernate source code can be found on sourceforge. The easiest way to get hold of it is to use a Subversion (SVN) client such as TortoiseSVN to download the code from the repository. NHibernate lives at: https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/

Just run Nant in trunk/NHibernate directory to build. Check the options file if you want to see different ways of building.

Working with Visual Studio

You can also open the source using the supplied Visual Studio solution file; this allows you to run tests, submit patches etc. Ignore the 'customized project' warning as this is a trusted source.

Mapping with NHibernate

Initialization

There are four steps to getting NHibernate working:

Provide information to access the database

Configure log4net

Provide the mapping information of the entities and value types

Build the session factory

Accessing the Db

We need to modify our config file to tell our application how to initialize NHibernate. We can do this in a separate config file, or, my preferred option, from within the app.config file. You can copy the nhibernate-configuration.xsd file into the visual studio common7/packages/schemas/xml file to get intellisense for writing NHibernate configuration information.

We set four properties to access the database:

ConnectionProvider: This value never changes.

Dialect: This depends on your database

ConnectionDriver: This depends on your database.

ConnectionString: The string used to create a database connection.

 

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

<configuration>

  <configSections>

    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" />

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

  </configSections>

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">

    <session-factory>

      <property name="connection.provider">

        NHibernate.Connection.DriverConnectionProvider

      </property>

      <property name="dialect">

        NHibernate.Dialect.MsSql2000Dialect

      </property>

      <property name="connection.driver_class">

        NHibernate.Driver.SqlClientDriver

      </property>

      <property name="connection.connection_string">

        Initial Catalog=nhibernate;Integrated Security=SSPI

      </property>

    </session-factory>

  </hibernate-configuration>

</configuration>

This allows us to create a NHibernate SessionFactory. The SessionFactory is an expensive resource to initialize, so we only want one. Crudely for this example we just make it a static member of the class. But the key is to understand that you create the SessionFactory once, at start up, and then create sessions from the factory when you need to interact with persistent entities.

   public class Program

    {

        private static ISessionFactory sessionFactory = null;

 

        public static void Main(string[] args)

        {

            log4net.Config.XmlConfigurator.Configure();

           

            Configuration cfg = new Configuration();

            cfg.Configure();

 

            sessionFactory = cfg.BuildSessionFactory();

 

        }

Configuring log4net

NHibernate uses log4net for logging so it is worth setting up log4net so that you can see any errors that are generated. Configuration in NHibernate can be a little painful and frustrating so its worth getting all the feedback you can to help you through it. Again you configure log4net by adding to your applications's configuration file.

<configSections>

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

  </configSections>

 

<log4net>

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender, log4net">

      <layout type="log4net.Layout.PatternLayout, log4net">

        <param name="ConversionPattern" value="%m" />

      </layout>

    </appender>

    <root>

      <priority value="WARN" />

      <appender-ref ref="ConsoleAppender" />

    </root>

  </log4net>

Adding the following line to the application means that you do not need to re-start the application to make configuration changes:

[assembly: log4net.Config.XmlConfigurator(Watch=true)]

We must add a reference to the library log4net.dll (it is available with the binaries of NHibernate).

We call log4net.Config.XmlConfigurator.Configure() before starting to configure NHibernate. This call activates log4net.

log4net.Config.XmlConfigurator.Configure();

Next Time

We will look at how we map entities

 


Posted 12-01-2008 1:40 PM by Ian Cooper

[Advertisement]

Comments

Tuna Toksoz wrote re: Introduction to NHibernate, Part 1
on 12-01-2008 9:36 AM

Ian, you also need to specify the proxyfactory for trunk.

Ed McPadden wrote re: Introduction to NHibernate, Part 1
on 12-01-2008 1:10 PM

Thanks Ian...I am just starting to look at evaluating NHibernate so I find this post (and hopefully future posts) very useful!

Reflective Perspective - Chris Alcock » The Morning Brew #235 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #235
on 12-02-2008 4:58 AM

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #235

2008 December 02 - Links for today « My (almost) Daily Links wrote 2008 December 02 - Links for today &laquo; My (almost) Daily Links
on 12-02-2008 5:11 AM

Pingback from  2008 December 02 - Links for today &laquo; My (almost) Daily Links

Dew Drop - December 2, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - December 2, 2008 | Alvin Ashcraft's Morning Dew
on 12-02-2008 8:44 AM

Pingback from  Dew Drop - December 2, 2008 | Alvin Ashcraft's Morning Dew

application program interface | Digg hot tags wrote application program interface | Digg hot tags
on 12-02-2008 5:15 PM

Pingback from  application program interface | Digg hot tags

Ryan wrote re: Introduction to NHibernate, Part 1
on 12-05-2008 12:02 PM

Ed be sure to check out Steve Bohlen's www.summerofnhibernate.com too.  It's a very exhaustive look at Nhibernate in a screencast series...it's presented in a style that really lends itself to a fairly deep understanding of different Nhibernate patterns and theory.

eKnacks wrote re: Introduction to NHibernate, Part 1
on 12-06-2008 6:47 AM

You've been knacked. Keep up the good work.

WOMBAT wrote Useful Links
on 12-06-2008 12:01 PM

Useful Links

Ian Cooper [MVP] wrote Introduction to NHibernate, Part 2
on 12-12-2008 4:06 AM

Last time we introduced setting up the configuration file . This time we will look at the basics of mapping

Community Blogs wrote Introduction to NHibernate, Part 2
on 12-12-2008 4:58 AM

Last time we introduced setting up the configuration file . This time we will look at the basics of mapping

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-13-2008 2:02 AM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-14-2008 1:02 PM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-18-2008 10:00 AM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-18-2008 10:01 AM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-18-2008 11:04 AM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-18-2008 12:01 PM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-18-2008 1:22 PM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-18-2008 2:01 PM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-18-2008 3:01 PM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-18-2008 4:01 PM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-18-2008 5:06 PM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-18-2008 6:01 PM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-19-2008 6:05 AM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-19-2008 7:06 AM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas wrote Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas
on 12-19-2008 8:02 AM

Pingback from  Introduction to NHibernate, Part 2 - taccato! trend tracker, cool hunting, new business ideas

Invalid Argument » Introduction to NHibernate wrote Invalid Argument &raquo; Introduction to NHibernate
on 01-22-2009 9:30 PM

Pingback from  Invalid Argument &raquo; Introduction to NHibernate

Add a Comment

(required)  
(optional)
(required)  
Remember Me?