考慮下面的類結構...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
得到正確的記錄都Actions
和ListViewActions
,但是有相同記錄的多個條目。記錄數量等於對ControlAction
表格的聯合數量,在這種情況下爲2。
我該如何避免這種情況?如果我從listViews
查詢中刪除SetFetchMode
,則會通過我不想要的代理懶惰地加載操作。
我還試圖創建別名...
.SetFetchMode("Actions", FetchMode.Eager)
.CreateAlias("Actions","actions",JoinType.RightOuterJoin)
.SetFetchMode("ListViewActions",FetchMode.Eager)
.CreateAlias("ListViewActions", "liactions", JoinType.RightOuterJoin)
這去除重複的條目,但並沒有急於負載
你應該總是語言標記您的問題。 – 2010-05-21 15:50:46
@Warren - 謝謝 – Zuber 2010-05-21 16:02:52