NHibernate:如何测试多对一关系是懒惰的?

时间:2010-05-20 19:40:56

标签: nhibernate nhibernate-mapping

我有一个简单的多对一关系(订单有一个客户放置它)。这是我的订单映射的除外:

<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 个答案:

答案 0 :(得分:1)

我在看源头,似乎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;
            }

因此,它取决于您使用的版本,但如果您将其映射为noproxy(即UnwrapProxy),则看起来2009版本仅对此属性返回true。这给你带来了什么?

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

to1.IsLazy.ShouldBeTrue();
to1.UnwrapProxy.ShouldBeFalse();
相关问题