2009-12-23 27 views
0

我有一個對象有許多關聯到其他對象。所有這些都是由nHibernate懶散地提取的,幾乎在所有情況下都很好。如何一次性在nHibernate ICriteria的所有關聯中設置Fetchmode?

在特定情況下,在這種情況下導出大量記錄,我想將Fetchmode設置爲渴望所有關聯。有沒有辦法做到這一點,而無需手動逐一指定:

ICriteria crit = CreateCriteria(). 
    .SetFetchMode("Address", FetchMode.Eager) 
    .SetFetchMode("ContactPerson", FetchMode.Eager); 

我想找到,但一直沒能到的方法:

// This doesn't work. 
ICriteria crit = CreateCriteria().SetFetchMode(FetchMode.Eager); 

回答

1

不,沒有沒有辦法以一攬子的方式做到這一點。

+0

這是我的恐懼......我希望有人證明我錯了。 – 2009-12-23 08:25:44

+0

唉......這是事實。我使用了一種不同的方法,逐個獲取記錄並追加到導出,在兩者之間沖洗會話。現在,服務器不再耗盡內存。這至少是我們實施的快速解決方案,以便獲得該版本:P – 2010-01-06 14:00:00

2

你可以嘗試使用NHibernate元數據。

ISessionFactory sessionFactory; 

Type type = typeof(MyEntity); 
IClassMetadata meta = sessionFactory.GetClassMetadata(type); 
foreach (string mappedPropertyName in meta.PropertyNames) 
{ 
    IType propertyType = meta.GetPropertyType(mappedPropertyName); 
    if (propertyType.IsAssociationType) 
    { 
     // initialize property 
     // recursively go through the properties of the associated entity 
    } 

    if (propertyType.IsCollectionType) 
    { 
     // initialize collection 
     // if it is a collection of entities, 
     // recursively go through the properties of the associated entity 

     // Use NHibernateUtil.Initialize 
    } 
} 

我不確定這是否值得付出努力。

+0

謝謝。我在無法訪問sessionfactory的級別執行操作,所以我懷疑這個Appraoch對我有什麼好處。但我會研究MetaData! – 2009-12-23 09:58:25

+0

你可以在知道會話工廠的地方實現這個全部的東西,並通過接口將其提供給其他部分。 – 2009-12-23 10:16:11

相關問題