NHibernate: how to enable lazy loading on one-to-one mapping

nhibernatenhibernate-mapping

One-to-one relations within nhibernate can be lazyloaded either "false" or "proxy". I was wondering if anyone knows a way to do a lazy one-to-one mapping.

I worked out a hack to achieve the same result by using a lazy set mapped to a private field, and having the public property return the first result of that set. It works, but isn't the cleanest code…

Thanks in advance!

Best Answer

Lazy loading of one-to-one isn't supported unless the association is mandatory. See here for the reasoning.

It boils down to the fact that in order to decide if the other side of the relationship exists (N)Hibernate has to go to the database. Since you've already taken the database hit, you might as well load the full object.

While there are cases where hitting the DB just to see if the related object exists without actually loading the object makes sense (if the related object is very "heavy"), it isn't currently supported in NHibernate.

Related Topic