Yet another post on my learning NHIbernate in public…. In an attempt to provide full disclosure I am a complete NOOBIE when it comes to NHibernate. In fact I have only been using it for a few weeks or so. What this means is take everything I say in this post with a grain of salt cause there may be a better, easier way.
Today when trying to run some full end to end tests on my data mappings I ran into a pesky little issue. One of my many-to-one mappings did not return a value out of the database. I fully expected this situation so I was testing for null refs in my code. However, by default NHibernate will not provide a null object, instead it will provide a ObjectNotFoundException on the proxy for that object. This is a bit painful as you cannot really test for this. The good news is that there is a way to resolve this issue.
All you need to do is provide not-found="ignore" attribute on your mappings. This tells NHibernate to return a null in place of the proxy.
If you are using traditional .hbm mapping files you can add this attribute as such:
<many-to-one access="field.camelcase-underscore" not-found="ignore" name="InsuranceType">
If you are using FluentNhibernate you can provide it as such:
References( x => x.InsuranceType )
.Access.AsCamelCaseField( Prefix.Underscore )
.SetAttribute( "not-found", "ignore" );
By adding the not-found attribute you should be able to get around this issue. Hope this helps.
Till next time,
01-29-2009 6:57 AM