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 [...]