2010-05-20 59 views
1

我有一個簡單的多對一關係(一個訂單有一個客戶放置它)。這裏是除了從我的次序來映射:NHibernate:如何測試多對一的關係是懶惰的?

<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(); 

是怎麼回事?

+0

難道你不想在名爲「Customer」的屬性上檢查IsLazy嗎? – dotjoe 2010-05-20 19:55:38

+0

你是對的 - 我只是輸入錯誤 - 錯誤仍然發生。 Editted。 – 2010-05-20 20:03:59

+0

客戶是什麼樣的? (代碼) – 2010-05-20 20:19:56

回答

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 2009年的版本僅此屬性返回true(即UnwrapProxy )。這給你什麼?

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

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