Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Many-to-One mapping throwing a ObjectNotFoundException, and how to resolve it

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,


Posted 01-29-2009 6:57 AM by Derik Whittaker

[Advertisement]

Comments

Martin Watts wrote re: Many-to-One mapping throwing a ObjectNotFoundException, and how to resolve it
on 01-29-2009 9:07 AM

I never have used NHibernate (LLBLGen on the other hand.... ), but one question immediately pops up when I see your sample code: aren't there any constants or enumerations for those strings??!

Derik Whittaker wrote re: Many-to-One mapping throwing a ObjectNotFoundException, and how to resolve it
on 01-29-2009 9:16 AM

@Martin,

As the Fluent interface on the mapping continues to grow, these 'magic string' values will be going away.

If you are dealing with the xml directly then no, there is not.

DotNetKicks.com wrote Many-to-One mapping throwing a ObjectNotFoundException, and how to fix
on 01-29-2009 9:50 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetShoutout wrote Many-to-One mapping throwing a ObjectNotFoundException, and how to resolve it - Derik Whittaker
on 01-29-2009 2:56 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Reflective Perspective - Chris Alcock » The Morning Brew #276 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #276
on 01-30-2009 3:32 AM

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #276

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)