Setting up NHibernate 2.0

Posted on 23rd of July 2008

I’ve spent several long hours over the last week, trying to get Hibernate for .NET to even just work in Visual Studio. The changed configuration format has been, since I’m not seeing any real examples of how to setup NHibernate 2.0 (Beta 2, which will probably be going release later this month) on the net, I’m just putting it out there now, since I’m trying to learn the whole framework, and so far this was my biggest stumbling point.

Important to note that this isn’t a true example by any measure. It merely shows what to put in your App.config (especially since the official documentation mostly covers the Java version, which naturally has no idea what App.config is. And what little there is for NHibernate, is for the last release.

The example is very simple, it mainly shows:

  • The contents of App.config

  • An example *.hbm.xml file for the sample User class.

  • If you should wish, the whole Visual Studio 2008 Solution file

I used Visual C# 2008 Express Edition and for the database I used SQL Server Express 2005 (make sure to get both “Microsoft SQL Server 2005 Express Edition ” and “SQL Server Management Studio Express”, the latter part is the GUI to manage the database, unless talking pure SQL into one end of a pipe satisfies you).

Download the entire VS 2008 Solution.

Configs, etc

The App.config looks like

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration"
             type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory>
      <property name="dialect">
        NHibernate.Dialect.MsSql2005Dialect
      </property>
      <property name="connection.provider">
        NHibernate.Connection.DriverConnectionProvider
      </property>
      <property name="connection.driver_class">
        NHibernate.Driver.SqlClientDriver
      </property>
      <property name="connection.connection_string">
        Server=SVENDSDESKTOP\SQLEXPRESS;
        Database=NHibernate;
        Integrated Security=True;
      </property>
    </session-factory>
  </hibernate-configuration>
</configuration>

The NHibernateTest.User.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateTest">
  <class name="User, NHibernateTest" table="users">
    <id name="Id" column="LogonId" type="String" length="20">
      <generator class="assigned" />
    </id>
    <property name="UserName" column="Name" type="String" length="40"/>
    <property name="Password" type="String" length="20"/>
    <property name="EmailAddress" type="String" length="40"/>
    <property name="LastLogon" type="DateTime"/>
  </class>
</hibernate-mapping>

And the startup code is the following.

static void Main(string[] args)
{
    Configuration cfg = new Configuration();
    cfg.AddXmlFile("NHibernateTest.User.hbm.xml");

    ISessionFactory factory = cfg.BuildSessionFactory();
    ISession session = factory.OpenSession();
    ITransaction transaction = session.BeginTransaction();

    User newUser = new User();
    newUser.Id = "joe_cool";
    newUser.UserName = "Joseph Cool";
    newUser.Password = "abc123";
    newUser.EmailAddress = "joe@cool.com";
    newUser.LastLogon = DateTime.Now;

    // Tell NHibernate that this object should be saved
    session.Save(newUser);

    // commit all of the changes to the DB and close the ISession
    transaction.Commit();
    session.Close();
}

Annnd, the SQL needed would be the following. Note that the database used is “NHibernate”.

USE [NHibernate]
GO
/****** Object:  Table [dbo].[users]    Script Date: 07/23/2008 22:32:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[users](
	[LogonID] [nvarchar](20) NOT NULL DEFAULT ('0'),
	[Name] [nvarchar](40) NULL DEFAULT (NULL),
	[Password] [nvarchar](20) NULL DEFAULT (NULL),
	[EmailAddress] [nvarchar](40) NULL DEFAULT (NULL),
	[LastLogon] [datetime] NULL DEFAULT (NULL),
PRIMARY KEY CLUSTERED
(
	[LogonID] ASC
)WITH (PAD_INDEX  = OFF,
       STATISTICS_NORECOMPUTE  = OFF,
       IGNORE_DUP_KEY = OFF,
       ALLOW_ROW_LOCKS  = ON,
       ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Tags: , , ,

14 Responses to “Setting up NHibernate 2.0”

  1. So you want to learn NHibernate? - Part 1 of 1, The Links « HSI Developer Blog Says:

    [...] first NHibernate based applicationSetting up NHibernate 2.0Using NHibernate as an ORM Solution for [...]

  2. So you want to learn NHibernate? (or, NHibernate Hyperlink Acupuncture) | The Freak Parade Says:

    [...] first NHibernate based application Setting up NHibernate 2.0 Using NHibernate as an ORM Solution for [...]

  3. Svend Says:

    Hey Svend

    Thank you for the example, works perfectly with newly released NHibernate.

    In a Client/Server architecture, would you publish ISessionFactory to the client or would you write your own code ?

    E.g. You have distributed application to create users…

    Wkr.
    Svend

    PS. When i downloaded the source i was a bit confused about the name of the server, until it striked me that i wasn’t the only one in the world with such a great name.

  4. Dave Wallin Says:

    Thanks alot for this posting. I looked everywhere for an example of this.

  5. Warren Says:

    Thanks, this article helped me ALOT!!! I just couldnt figure out how to get the example on hibernate.org to actually work.

  6. Configuration changes in NHibernate 2.0 « Khac Ghi Says:

    [...] Moral of the story is to check the source first — the NHibernate.Examples folder is filled with helpful goodies. Svend Tofte also has a helpful post on setting up NHibernate 2.0. [...]

  7. Configuration changes in NHibernate 2.0 « IT Staff Says:

    [...] Moral of the story is to check the source first — the NHibernate.Examples folder is filled with helpful goodies. Svend Tofte also has a helpful post on setting up NHibernate 2.0. [...]

  8. Jeff Larcomb Says:

    Thank you!!! Why was this such a well kept secret? I’m glad you didn’t keep this to yourself ;^)

  9. QuachNguyen Says:

    Thank svend@svendtofte.com,

    I’m looking for this example in a while, It’s very helpful.

    Thanks,
    QuachNguyen

  10. Dudo Says:

    Thank you for the example!
    Really nice and clean.

    Thanks,
    Dudo

  11. Aniruddha Says:

    This was great. Finally my NHibernate thing works. Didn’t know (and I still don’t quite understand) that the hibernate.cfg.xml has to be treated as an app.config file. Why is that really the case?

  12. Svend Says:

    Well, I presume it’s in order to fit into the .NET enviroment, which does make sense. It’s just annoying that the NHibernate (when I used it these months ago) actually just tells you to go look at the Java version, when the config file setup differs substantially. I’m glad to hear that this post is still helpful, since it was made with the RC and not the final release.

  13. Marco Pankow Says:

    Nice example Svend, thanks for posting.

  14. Dave Says:

    Thank you. This has been most helpful.
    D.

Leave a Reply