I have quite a problem with NHibernate. I have a reference from Table1 to Table2, and I want NHibernate to, when a corresponding record is not found in Table2, to not then issue a SELECT statement against Table2, to, I don't know, make really really sure that it actually isn't there.
I've tried adding modifiers like .LazyLoad(Laziness.False)
and .NotFound.Ignore()
to my reference, but NHibernate gaily ignores my commands with extreme prejudice, issuing its select and breaking my code.
It is correct, that NHibernate tries to load "not existing". It must do that.
As stated here Ayende - NHibernate Mapping (an extract):
12) not-found is another legacy feature, it controls how NHibernate
behaves when it finds an invalid foreign key. That is, a value that
points to an entity that doesn’t exist. By default, this would trigger
an error, as this generally indicate a problem with the database, but
with legacy database, you can tell it to set the property value to
null instead.
And as could be found here: Lazy loading for NHibernate with Ignore.NotFound (an extract):
When you specify the .NotFound().Ignore() this forces the entity to be
eagerly loaded and cannot be overriden with the .LazyLoad().
NHibernate does this because it has to be sure that relationship
exists or doesn't exist since you are not relying on the database to
enforce this.
And here Why Nhibernate won't lazy load my many-to-one relationship?, José F. Romaniello says:
This is your problem, nhibernate must to be sure, that an invoice
EXIST or do not exist for each enrollment.
I'd strongly recommend you to fix your data problems and remove the
not-found="ignore" attribute. It is a bad thing.
try
.Not.LazyLoad();
instead of .LazyLoad(laziness.false);