NHibernate Thinks Your Enum is Dirty

Get your head out of the gutter.

It’s important to look at your mapping files closely when trying to diagnose unexpected results.  One of our developers had recently added two properties to our big honkin’ CustomerOrder entity.  These both happened to be enumeration values, which are represented by Int values in the database.  Of course, NHibernate mapping files (hbm’s) are from the POCO perspective, which means the <property type=”…” />  value should be the Enumeration’s themselves.  If you use Int32, though, things will appear to work, but you’re actually causing NHibernate to do an incompatible type comparison during it’s “Dirty” checks.  This will cause your lazily, loaded entities to be dirty as soon as you get them.

So, be careful in reviewing your mappings, because it doesn’t take much to screw up something small that has a big impact.

NHibernate Query Generator Installer for Visual Studio 2008

imageI posted recently a manual .reg file for adding the NHibernate Query Generator custom tool to Visual Studio 2008.  I just sent Ayende an update to the .Setup Product.wxs, a WiX file, which adds a new fixture specifically for 2008. 

If you are interested in building the installer and running the trunk of NHQG, below is my batch script.  Obviously, you’ll have to change a couple of paths, but it should get you started. 

You can download the Product.wxs pending Ayende approving the new Product.wxs file here.

echo Off
set path=%path%;c:\tools\svn-win32-1.4.4\bin

echo get Trunk (Non-Recursive)…
svn co
https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk trunk -q -N
echo done

echo get Art…
svn co
https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk/Art trunk/Art -q
echo done

echo get SharedLibs…
svn co
https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk/SharedLibs trunk/SharedLibs -q
echo done

echo get NHQG…
svn co
https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk/NHibernate.Query.Generator trunk/NHibernate.Query.Generator -q
echo done

echo build NHQG…
pushd .
cd trunk\NHibernate.Query.Generator
%windir%\Microsoft.NET\Framework64\v2.0.50727\msbuild.exe default.build
echo done

echo build Installer
cd NHibernate.Query.Generator.Setup
%windir%\Microsoft.NET\Framework\v2.0.50727\msbuild.exe Setup.wixproj
echo done

NHibernate Query Generator with Visual Studio 2008

Custom Tool Configuration I’ve been using Ayende’s NHibernate Query Generator (NHQG) to help type-ify my NHibernate repository queries.  One of the great +1’s of his tool is that you can apply it as a Custom Tool for your NHibernate mapping files right within Visual Studio.  

With this setup, every change to my mapping file is reflected in my typed NHibernate ICriteria instances.  For those who work directly with Hibernate Expressions, look how clean my queries are:

// Get it using the Id
public static MyClass GetById(long id)
return Repository.Get<MyClass>(Where.MyClass.Id == id);

// Get those updated today
public void Example2()
IList<MyClass> cards = Repository.Find<MyClass>(Where.MyClass.LastUpdated >= DateTime.Today);

That’s just awesome!

There’s a slight glitch though, his installer only registers the tool for Visual Studio 2005.  I’m sure now that 2008 is RTM (I’ve been using Beta 2) he’ll soon have a new version out, but in the mean time, you can use these two .reg files:

NHQG C# Custom Tool Registration

NHQG VB Custom Tool Registration