Archive, NHibernate

Migrating to NHibernate 2.0

Jason / November 8, 2008

The NHibernate documentation is often very handy when working on NHibernate based project. I keep PDF copies of the documentation to NHibernate 1.0 and 1.2 on my hard drive as I never know when I will need to reference them. Unfortunately the NHibernate team seems to have dropped the ball in regards to version 2.0. Not only does the NHibernate website lack any documentation on version 2.0, the website acts as if version 2.0 hasn’t even been released yet. Unlike previous updates to the framework, version 2.0 represents a complete refactoring of the code base with many namespaces and objects either removed or renamed. Despite the lack of official documentation there are still several resources to help you migrate your existing NHibernate 1.2 application to NHibernate 2.0.

List of breaking changes

The NHibernate forum has a thread discussing the breaking changes between version 1.2 and an alpha pre-release of 2.0. For the complete list check out the forum, however here are the ones you will be most likely to face.

Problem: The NHibernate.Expression namespace no longer exists.

Overview: Most notable of the items formerly kept in this namespace was the Expression object used when creating ICriteria. Fortunately the object still existing in the NHibernate.Criterion namespace.

Solution: Replace all “using” statements to reflect the namespace change.

//using NHibernate.Expression; //this line is no longer needed in NH 2.0
using NHibernate.Criterion;    //replace it with this line.

Problem: The nhibernate configuration section (most likely in your web.config or app.config) is no longer used.

Overview: NHibernate 2.0 no longer looks at the configuration section named nhibernate, instead it looks for a configuration section called hibernate-configuration (notice the lack of an “n” in hibernate). Moreover the configuration section wasn’t simply renamed, it bears and entirely different schema.

Solution: In short the entire configuration section is going to have to be reworked slightly.

For starters your configuration section declaration will have to change its name and type:

<configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" requirePermission="false"/> </configSections> read more

Archive, C#, NHibernate

NHibernate Formula Properties

Jason / February 19, 2008

One often overlooked feature of NHibernate is the use of formula properties. Formula properties are properties that do not map to columns in the database, but instead are mapped using raw SQL queries.

The formula is mapped in the NHibernate mapping file using the same property element that normal properties use, just replacing the column keyword for the formula keyword.

<property name="FormulaPropertyName" formula="SQL STATEMENT" /> 

The basic idea is that when NHibernate loads your object it will issue your custom SQL at the same time, allowing for complex read-only properties to offload their work to the database.

For example, assume you have an class representing an order on an e-commerce website. An order is typically composed of line items, and a common question to ask the order as a whole is “what is your subtotal?”. Without weighing the pros and cons of such a decision assume that you do not want to store the subtotal as a field on the order table.

A perfectly acceptable solution would be to have a read-only property as such:

public decimal SubTotal{ get{ decimal subTotal = 0; foreach(LineItem li in this.LineItems){ subTotal += li.ExtendedPrice; //i.e., units ordered times unit price. } return subTotal; } } read more

Archive, NHibernate

NHibernate and Text fields

Jason / October 11, 2006

In my last post about NHibernate I mentioned using NHibernate.Mapping.Attributes to simplify things.
Since my last post I have had the need to store rather large text data in a SQL Server Text field. My initial attempt was to simply create a C# string property that represented the field as such:

public string SomeData {
    get { return _someData; }
    set { _someData = value; }

I was shocked to learn that the above did not work correctly. What I was experiencing was NHibernate was silently truncating my data to 3000 characters (or maybe it was 4000, I can’t remember exactly). The reason for this appears to be that NHibernate assumes the field type to be of varchar not Text.

The solution to this problem is to inform NHibernate of the field’s true type. As it turns out this can be done by specifying it as type ‘StringClob’.

[NHMA.Property(Name="SomeData",Type="StringClob")] public string SomeData { get { return _someData; } set { _someData = value; } } read more