Setting up NHibernate 2.0
Posted on 23rd of July 2008I’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.xmlfile for the sampleUserclass. -
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: hibernate, nhibernate, or mapping, Visual Studio

August 1st, 2008 at 12:26 am
[...] first NHibernate based applicationSetting up NHibernate 2.0Using NHibernate as an ORM Solution for [...]
August 8th, 2008 at 9:25 pm
[...] first NHibernate based application Setting up NHibernate 2.0 Using NHibernate as an ORM Solution for [...]
August 29th, 2008 at 6:54 am
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.
September 8th, 2008 at 6:58 pm
Thanks alot for this posting. I looked everywhere for an example of this.
October 15th, 2008 at 2:45 pm
Thanks, this article helped me ALOT!!! I just couldnt figure out how to get the example on hibernate.org to actually work.
October 20th, 2008 at 9:41 am
[...] 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. [...]
October 26th, 2008 at 1:34 pm
[...] 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. [...]
May 7th, 2009 at 5:47 pm
Thank you!!! Why was this such a well kept secret? I’m glad you didn’t keep this to yourself ;^)
May 25th, 2009 at 5:35 pm
Thank svend@svendtofte.com,
I’m looking for this example in a while, It’s very helpful.
Thanks,
QuachNguyen
September 15th, 2009 at 4:51 pm
Thank you for the example!
Really nice and clean.
Thanks,
Dudo
September 21st, 2009 at 7:11 pm
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?
September 22nd, 2009 at 1:24 pm
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.
December 12th, 2009 at 3:36 pm
Nice example Svend, thanks for posting.
February 3rd, 2010 at 2:09 pm
Thank you. This has been most helpful.
D.