2010-05-21 138 views
1

考慮下面的類結構...NHibernate的上一個表連接兩次

public class ListViewControl 
{ 
    public int SystemId {get; set;} 
    public List<ControlAction> Actions {get; set;} 
    public List<ControlAction> ListViewActions {get; set;} 
} 

public class ControlAction 
{ 
    public string blahBlah {get; set;} 
} 

我想使用NHibernate加載類ListViewControl 熱切。 映射使用流利的如下圖所示

public UIControlMap() 
    { 
     Id(x => x.SystemId); 
     HasMany(x => x.Actions) 
      .KeyColumn("ActionId") 
      .Cascade.AllDeleteOrphan() 
      .AsBag() 
      .Cache.ReadWrite().IncludeAll(); 
     HasMany(x => x.ListViewActions) 
      .KeyColumn("ListViewActionId") 
      .Cascade.AllDeleteOrphan() 
      .AsBag() 
      .Cache.ReadWrite().IncludeAll(); 
    } 

這是我正在試圖急切地加載它

var baseActions = DetachedCriteria.For<ListViewControl>() 
      .CreateCriteria("Actions", JoinType.InnerJoin)     
      .SetFetchMode("BlahBlah", FetchMode.Eager) 
      .SetResultTransformer(new DistinctRootEntityResultTransformer()); 

var listViewActions = DetachedCriteria.For<ListViewControl>() 
      .CreateCriteria("ListViewActions", JoinType.InnerJoin) 
      .SetFetchMode("BlahBlah", FetchMode.Eager) 
      .SetResultTransformer(new DistinctRootEntityResultTransformer()); 

var listViews = DetachedCriteria.For<ListViewControl>() 
      .SetFetchMode("Actions", FetchMode.Eager) 
      .SetFetchMode("ListViewActions",FetchMode.Eager) 
      .SetResultTransformer(new DistinctRootEntityResultTransformer()); 

var result = _session.CreateMultiCriteria() 
       .Add("listViewActions", listViewActions) 
       .Add("baseActions", baseActions) 
       .Add("listViews", listViews) 
       .SetResultTransformer(new DistinctRootEntityResultTransformer()) 
       .GetResult("listViews"); 

現在,我的問題是,類ListViewControl得到正確的記錄都ActionsListViewActions,但是有相同記錄的多個條目。記錄數量等於對ControlAction表格的聯合數量,在這種情況下爲2。

我該如何避免這種情況?如果我從listViews查詢中刪除SetFetchMode,則會通過我不想要的代理懶惰地加載操作。

我還試圖創建別名...

.SetFetchMode("Actions", FetchMode.Eager) 
.CreateAlias("Actions","actions",JoinType.RightOuterJoin) 
.SetFetchMode("ListViewActions",FetchMode.Eager) 
.CreateAlias("ListViewActions", "liactions", JoinType.RightOuterJoin) 

這去除重複的條目,但並沒有急於負載

+0

你應該總是語言標記您的問題。 – 2010-05-21 15:50:46

+0

@Warren - 謝謝 – Zuber 2010-05-21 16:02:52

回答

0

這是一個非常低效的方式渴望負載集合。這裏有一個更好的辦法:

http://ayende.com/Blog/archive/2010/01/16/eagerly-loading-entity-associations-efficiently-with-nhibernate.aspx

+0

我以爲我使用了同樣的機制... http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/06/eager-loading-aggregate-with-many-child-collections .aspx 您提供的URL和我爲單個實體對象加載集合的URL。我想檢索所有對象。 看看這個網址以及 http://stackoverflow.com/questions/2839357/load-collections-eagerly-in-nhibernate-using-criteria-api – Zuber 2010-05-21 21:16:05