NHibernate: Как проверить отношения многих к одному ленив? - PullRequest
1 голос
/ 20 мая 2010

У меня простое отношение «многие к одному» (у Заказа есть Клиент, который его разместил) Вот исключением из моего отображения заказа:

<many-to-one name="Customer" column="FK_CUSTOMERS" class="MyApp.Customer, MyApp" 
not-null="true" lazy="proxy" cascade="none" />

Пока не проходит следующее:

configuration.GetClassMapping(typeof(Order))
  .ReferenceablePropertyIterator.FirstOrDefault(p=>p.Name=="Customer")
  .IsLazy
  .ShouldBeTrue();

Что дает?

1 Ответ

1 голос
/ 21 мая 2010

Я смотрел на источник, и кажется, что NH рассматривает IsLazy как-то странно. Здесь было последнее изменение 2010-01-26

get
            {
-               if (propertyValue is ToOne)
-               {
-                   // both many-to-one and one-to-one are represented as a
-                   // Property.  EntityPersister is relying on this value to
-                   // determine "lazy fetch groups" in terms of field-level
-                   // interception.  So we need to make sure that we return
-                   // true here for the case of many-to-one and one-to-one
-                   // with lazy="no-proxy"
-                   //
-                   // * impl note - lazy="no-proxy" currently forces both
-                   // lazy and unwrap to be set to true.  The other case we
-                   // are extremely interested in here is that of lazy="proxy"
-                   // where lazy is set to true, but unwrap is set to false.
-                   // thus we use both here under the assumption that this
-                   // return is really only ever used during persister
-                   // construction to determine the lazy property/field fetch
-                   // groupings.  If that assertion changes then this check
-                   // needs to change as well.  Partially, this is an issue with
-                   // the overloading of the term "lazy" here...
-                   ToOne toOneValue = (ToOne)propertyValue;
-                   return toOneValue.IsLazy && toOneValue.UnwrapProxy;
-               }
+               // NH Specific: Hibernate doesn't make a distinction between
+               // lazy and no-proxy, but NHibernate does. While Hibernate tracks
+               // just whatever a property is lazy, we need to track lazy/no-proxy seperatedly.
+               
                return isLazy;
            }

Таким образом, это зависит от используемой версии, но похоже, что версии 2009 года возвращают true для этого свойства, только если вы сопоставили его как noproxy (т.е. UnwrapProxy). Что это дает вам?

var to1 = configuration.GetClassMapping(typeof(Order))
  .ReferenceablePropertyIterator.FirstOrDefault(p=>p.Name=="Customer")
  .Value as NHibernate.Mapping.ToOne;

to1.IsLazy.ShouldBeTrue();
to1.UnwrapProxy.ShouldBeFalse();
...