How to (n)hibernate
I love Hibernate. Yes I do. I like the abstraction layer Hibernate creates on top of the database so we deal with objects instead of ugly sql.
I've used Hibernate quite a few times and I never had any issues with getting it up and running. Just pop the jar into your /lib directory, create your mapping files and the corresponding pojos, then add the hibernate.cfg.xml to tell Hibernate how to get to your tables, and then you're all good to go.
nHibernate, however, has been causing me quite a lot of pain. Getting it up and running wasn't as easy as I thought it should've been.
To get started with nHibernate with a table called UserContactInfo, you will need the following:
- Download nHibernate and copy all the dlls that are included in the binary into your lib directory, or wherever it is that you dump your dlls. I made the mistake of only including nHibernate.dll and it went on to complain that it was missing log4net.dll and a whole host of other ones that I didn't think I needed.
- A hibernate mapping file,
UserContactInfo.hbm.xml. Double check the schema definitionnhibernate-mapping.xsdfile that was included in your download to make sure that everything matches up, i.e. version, namespace, etc. To enable intellisense for mapping and configuration files, copy the appropriate .xsd files to <VS.NET installation directory>\Common7\Packages\schemas\xml or in my case, I put them in <VS.NET installation directory>\Xml\Schemas - The corresponding pojo,
UserContactInfo.cs. You must override the Equals and GetHashCode methods and make all the getters and setters for the fields that you are mapping virtual so nHibernate can manipulate them.
using System;
using System.Collections.Generic;
using System.Text;
namespace CommKit
{
public class UserContactInfo
{
private String id;
private String contactId;
private Int32 numberOfMessagesReceived;
public UserContactInfo() { }
public virtual String Id {
get { return id; }
set { id = value; }
}
public virtual String ContactId {
get { return contactId; }
set { contactId = value; }
}
public virtual Int32 NumberOfMessagesReceived {
get { return numberOfMessagesReceived; }
set { numberOfMessagesReceived = value; }
}
public override bool Equals(object obj) {
if (obj == null) return false;
UserContactInfo userContactInfo = (UserContactInfo)obj;
if (this.Id != userContactInfo.Id || this.ContactId != userContactInfo.ContactId)
return false;
return true;
}
public override int GetHashCode() { return 1; }
}
} - The database table that it maps to
- nHibernate configuration settings to tell it how to connect to your database. This can be done with xml in the App.config file or programmatically when you're setting up your Configuration object, like so:
configuration.SetProperty("connection.provider", "NHibernate.Connection.DriverConnectionProvider");configuration.SetProperty("connection.driver_class", "NHibernate.Driver.MySqlDataDriver");configuration.SetProperty("connection.connection_string", "Server=hostname;Database=dbname;User ID=username;Password=password");configuration.SetProperty("dialect", "NHibernate.Dialect.MySQLDialect"); - Download MySQL Connection/Net for connecting to MySQL from .Net here. You should put the MySql.data.dll in either the GAC (usually
C:\windows\assembly) and add thequalifyAssemblyelement in application configuration file, specifying the full name of the assembly, OR, copy it into your application directory, ie.\bin\Release. - Then you can write code like this:
Configuration configuration = new Configuration();
configuration.AddAssembly("CommKit");
ISessionFactory factory = configuration.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
UserContactInfo userContactInfo = new UserContactInfo();
userContactInfo.Id = "uuid";
userContactInfo.ContactId = "contactId";
userContactInfo.NumberOfMessagesReceived = 5;
session.Save(userContactInfo);
transaction.Commit();
session.Close(); - Other things to remember, there are a few ways to tell nHibernate where to find your mapping files, one way is to add to the configuration object the name of the assembly where they are located. Like so:
configuration.AddAssembly("CommKit");All mapping files have to be set as an embedded resource. You can change that in the properties of the file itself.

Note: Using nHibernate 2.0.1, .NET framework 2.0, MySQL Connector/Net 5.2.5