Quick Tip: NullReferenceException, AliasToBeanTransformer, MultiQuery, and NHibernate

by Nicholas Piasecki on October 1st, 2009

When the following are all true with NHibernate 2.1, you’ll receive a NullReferenceException: Object reference not set to an instance of an object thrown from the innards of NHibernate’s AliasToBeanTransformer, mentioning some nonsense about a tuple:

  • You’re writing an HQL query.
  • You’re using aliases and the AliastoBeanTransformer.
  • You’re executing the HQL query via a MultiQuery.

The problem is that, for reasons that are not clear to me, the alias names get stripped when they’re used in a MultiQuery. This results in NHibernate telling the AliasToBeanTransformer something like “here, go ahead and set the property called NULL to 42.” And so it explodes when it references the property name string that is null.

If you just use CreateQuery() instead of a multiquery, however, the aliases work fine. So that is the workaround. Alternatively, you could use a constructor-style projection in your HQL, but I think that’s a pain.

In short: AliasToBeanTransformer simply does not work in a multiquery.

Happy NHibernating.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS