You can check out this article here http://sdesmedt.wordpress.com/2006/04/05/hello-world-with-nhibernate/
I can say it is really easy to get started. However if you follow the codes in there, you will start to realize to face problems getting it running.
I will provide some tips to get it work.
1) Requires Virtual on each property
As on original article:
public class MessageProvider
{
private int id;
private string message;
public MessageProvider()
{
}
public int Id
{
get { return id; }
set { id = value; }
}
public string Message
{
get { return message; }
set { message = value; }
}
}
Errors that you will get:
The following types may not be used as proxies:
TheData.MessageProvider: method get_Message should be virtual
TheData.MessageProvider: method get_Id should be virtual
TheData.MessageProvider: method set_Id should be virtual
TheData.MessageProvider: method set_Message should be virtual
Solution:
public class MessageProvider
{
private int id;
private string message;
public virtual int Id
{
get { return id; }
set { id = value; }
}
public virtual string Message
{
get { return message; }
set { message = value; }
}
}
You can remove the empty constructor. There is no reason to keep it there too.
2) Hibernate-Mapping namespace
I check in search engine and I realize a lot of people are frustrated about this. Easy. If you are using NHibernate 1.2.0.4000
As on original article:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-access="property">
Solution:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property">
Change it from 2.0 to 2.2. It solves the problem.
3) If you are using Microsoft SQL Server 2005
As on original article:
config.SetProperty(NHibernate.Cfg.Environment.Dialect, "NHibernate.Dialect.MySqlDialect");
Solution:
config.SetProperty(NHibernate.Cfg.Environment.Dialect, "NHibernate.Dialect.MsSql2005Dialect");
Just to make sure you have the right settings for your database, do check at this link http://www.hibernate.org/361.html?cmd=comphist&histnode=2604
There is no updates for Microsoft SQL Server 2005 but it worked the same with Microsoft SQL Server 2000.
Just to add that you can do this:
config.SetProperty(NHibernate.Cfg.Environment.ConnectionString, @"Server=wenchingpc\sql2005;initial catalog=nhibernate_test;Integrated Security=SSPI;");
config.SetProperty(NHibernate.Cfg.Environment.ConnectionString, @"Server=wenchingpc\sql2005;initial catalog=nhibernate_test;User ID=sa;Password=sa;");
The 2nd one is not part of the url above. But it will work just fine J
The connection string is very important especially to execute the code below:
IList messages = session.CreateCriteria(typeof(TheData.MessageProvider)).List();
4) How do you name the hbm.xml file?
You can name it anything that you want. As for this case, I have only one mapping file and a MessageProvider class. I will call it MessageProvider.hbm.xml.
Below is the actual source code:
** Take note, I will base on that article. So I have not implement any best practices. So readers who face problem from that article can refer it here.
MessageProvider.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace TheData
{
public class MessageProvider
{
private int id;
private string message;
public virtual int Id
{
get { return id; }
set { id = value; }
}
public virtual string Message
{
get { return message; }
set { message = value; }
}
}
}
MessageProvider.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property">
<class name="TheData.MessageProvider, HelloNHibernate" table="HelloWorld">
<id name="Id">
<generator class="identity" />
</id>
<property name="Message" />
</class>
</hibernate-mapping>
Program.cs (or MainClass.cs)
using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
namespace HelloNHibernate
{
class Program
{
static void Main(string[] args)
{
Configuration config = new Configuration();
config.SetProperty(NHibernate.Cfg.Environment.ConnectionProvider, "NHibernate.Connection.DriverConnectionProvider");
config.SetProperty(NHibernate.Cfg.Environment.Dialect, "NHibernate.Dialect.MsSql2005Dialect");
//config.SetProperty(NHibernate.Cfg.Environment.ConnectionString, @"Server=wenchingpc\sql2005;initial catalog=nhibernate_test;Integrated Security=SSPI;");
config.SetProperty(NHibernate.Cfg.Environment.ConnectionString, @"Server=wenchingpc\sql2005;initial catalog=nhibernate_test;User ID=sa;Password=!pandu@;");
config.AddAssembly("HelloNHibernate");
ISessionFactory factory = config.BuildSessionFactory();
try
{
ISession session = factory.OpenSession();
IList messages = session.CreateCriteria(typeof(TheData.MessageProvider)).List();
Console.WriteLine("No Messages: {0}\n", messages.Count);
foreach (TheData.MessageProvider aMessage in messages)
{
Console.WriteLine("Id: {0} - Message: {1}", aMessage.Id, aMessage.Message);
}
session.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
}
}
Hope you find it useful. Thank you.