Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to avoid NHibernate.ObjectNotFoundException when there is a foreign key but the referenced row does not exist?

Tags:

nhibernate

I am using NHibernate to pull some data out of a legacy db, and I have found several cases where there is a foreign key, but the referenced row has been removed.

When I make my NHibernate mapping (using Fluent NHibernate like so:

References(d => d.Group)
    .WithColumns("groupId", "dataset")
    .SetAttribute("lazy", "true");

I get an unitialized proxy for Group when loading the root object, followed by an ObjectNotFoundException when I attempt to use it.

If I disable lazy loading, I get an ObjectNotFoundException immediately when loading the root.

Therefore: Is there a way to have NHibernate make the Group null when loading the root? Or is it possible to check the unitialized proxy somehow, to see if it will succeed in loading the row?

like image 480
mookid8000 Avatar asked Mar 06 '09 12:03

mookid8000


1 Answers

I found the solution here - i needed to add

.SetAttribute("not-found", "ignore");

to the mapping.

like image 110
mookid8000 Avatar answered Oct 03 '22 17:10

mookid8000