Quick guide to nHibernate

nHibernate is a .NET port of the Java Hibernate system. The Hibernate system has been around for years in the open-source community. For this blog the terms are interchangeable.

Hibernate is a system that lets you persist your business entity objects into a persistent storage medium (database normally). So you write your business entity object, give it state and behaviour and then define an Xml file which instructs Hibernate on how to persist the class. (You could persist the class by serialising it, either to Xml or binary, but that only maps well to an object database, and even then it’s not ideal.)

With Hibernate you can define a table Employee, and a class Employee. The fields in the class persist into the columns in the table. All with a simple mapping file and a few method invocations.

You can also model relationships between your tables and classes. For example, an employee may work in a department so you define your Department table, model the FK/PK relationship in the DB, set the relationship in your Xml mapping file and voila, you have a collection class of Department objects available as a property of your Employee object.

Onto an example…

Department 0..* Employee
Employee *..* Computer

In the database, the tables are:

Employee: EmployeeId, Name, DepartmentId
EmployeeComputer: EmployeeId, ComputerId
Computer: ComputerId, Model
Department: DepartmentId, Name

In the code, the classes (public fields for ease of explanation):

[csharp]
class Employee {
public int Id;
public string Name;
public Department Department;
public Iesi.Collections.ISet Computers;
}

class Department {
public int Id;
public string Name;
public Iesi.Collections.ISet Employees;
}

class Computer {
public int Id;
public string Model
public Iesi.Collections.ISet Employees;
}
[/csharp]

Our user code can use syntax such as

[csharp]
Employee james = EmployeeFactory.GetStudent(1);
Console.WriteLn(“James is a member of the {0} department”, james.Department.Name);
Console.WriteLn(“James has {0} computers”, james.Computers.Count);
Computer newPC = new Computer();
newPC.Model = “P5”;
james.Computers.Add(newPC);
EmployeeFactory.SaveStudent(james);
[/csharp]

Our factory here has encapsulated the functionality of talking to the nHibernate classes. Let’s investigate that now.

[csharp]
class EmployeeFactory
{
private Configuration _cfg = null;
private ISessionFactory _factory = null;

public EmployeeFactory()
{
_cfg = new Configuration();
_cfg.AddAssembly(“Hibernate”);
_factory = _cfg.BuildSessionFactory();
}

public void GetEmployee(int id)
{
using (ISession session = _factory.OpenSession())
{
return (Employe)session.Load(typeof(Employee), id);
}
}

public void SaveEmployee(Employee employee)
{
using (ISession session = _factory.OpenSession())
{
Transaction tx = session.BeginTransaction();
session.Update(employee);
tx.Commit();
}
}
}
[/csharp]

So the only thing left is the Xml mapping files which instruct nHibernate how to map the C# classes onto the database tables. For each C# class we need an Xml file:

Employee.hbm.xml

[xml]

[/xml]

Department.hbm.xml

[xml]

[/xml]

Computer.hbm.xml

[xml]

[/xml]

Related articles:
http://www.theserverside.net/articles/content/NHibernate/NHibernate.html
http://www.theserverside.net/articles/content/NHibernateP2/NHibernateP2.html
http://www.xylax.net/hibernate/index.html
http://www.tobinharris.com/nhibernatefaq.aspx

Advertisements

2 thoughts on “Quick guide to nHibernate

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s