NHibernate 1.x : many-to-one not lazily loaded

2019-08-29 03:21发布

问题:

This is extract of my mapping file:

<class name="XXX.A"
         table="a"
         lazy="false">

     <many-to-one name="B"
                  lazy="proxy"
                  access="field.camelcase"
                  cascade="none"
                  not-null="false"
                  class="XXX.B" 
                  column="id_b"/>

 </class>

But when A is loaded by its ID, I see "left join" to fetch B. How to prevent this?

I may add that we use ISession.Get(...) and not ISession.Load(...).

回答1:

if you have <class name="XXX.B" lazy="false"> then NHibernate does not create a proxy class for it. Because of that all <many-to-one class="XXX.B" lazy="proxy|true" /> are ignored because NH can not create proxy objects.

To enable LazyLoading of B remove lazy="false" or set <class lazy="true">. If you disabled Lazy for classes because you dont want to make every method virtual you can also implement your own Proxyclass to handle the LazyLoading