— New online course!  Getting Started with Azure DevOps — Special launch price 50% off —

NHibernate and nullable value types.

by

If I were designing a database from scratch, I’d avoid nullable columns like The Plague.  Unfortunately, a lot of legacy (and not-so-legacy) databases tend to have nullable columns.  Kinda sucky if you’re doing NHibernate development under .NET 1.1 because you have to have to do a extra stuff to make it work. 


In preparation for my NHibernate talk at VSLive San Francisco, I needed to learn how to actually map that scenario.  Then I started thinking about the new nullable value type syntax in .NET 2.0 (int? myInt32 = null; DateTime? myDateTime = null;) and wondering well how that would work with NHibernate. 


So, I wrote up some unit tests and some sample classes to try it out. 


The .NET 1.1 way using the NHibernate nullable DLLs was a lot easier to work with than I expected.  Any value-type property on  your entity classes just needs to be the appropriate type from Nullables.dll (ex. DateTime –> NullableDateTime, int –> NullableInt32) and in the mapping file you have to add a “type“ attribute to the appropriate “property“ elements.


The nice thing is that when you access the properties on your entity classes, you really don’t have to be all that aware that they’re anything other than the regular .NET value type.  You can just assign to them and compare them pretty much as normal…no mucking around with new‘ing up instances of Nullable types in order to do assignments.  Example: temp.NullableBool = false;


The .NET 2.0 way is even less intrusive.  In your entity classes, mark the property datatype as nullable using the “?“ syntax and in your mapping file you don’t have do anything special at all.  It just works. 


Anyway, I’ve posted the test project and sample classes are here.  Make sure to check the database connection string in “hibernate.cfg.xml“ in the NHibernateResearch.VisualStudioTests project.  Also, since the tests do drop and create database tables, you might want to create a blank database and point hibernate.cfg.xml’s connection string to that new database. 


Enjoy.


-Ben


One Response to "NHibernate and nullable value types."
  1. I agree, I’m not super thrilled about using the Nullable.dll in 1.1, either. The nice thing is that it’s been replaced with 2.0 so we won’t have to deal with it all that much longer.

    I’ll have to check out that blog entry in a little more detail later. It looks like James Avery is saying that NHibernate doesn’t handle the "?" nullable syntax.

    That seems weird to me because my tests with "?" worked just fine with ordinary property mappings….no "type" required.

    Maybe I’m missing something or I’ve got errors in my tests.

    BUT! Even if you do have to do the 1.1 nullables.dll method, it’s not really an NHibernate specific problem or solution. Even if you weren’t using nhibernate, you’d still have to solve the problem of needing a nullable valuetype….that’s a limitation in Framework 1.1.

    -Ben

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: