简体   繁体   English

Nhibernate在表上加入两次

[英]Nhibernate join on a table twice

Consider the following Class structure... 考虑以下类结构...

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;}
}

I want to load class ListViewControl eagerly using NHibernate. 我想使用NHibernate 急于加载类ListViewControl。 The mapping using Fluent is as shown below 使用Fluent的映射如下所示

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

This is how I am trying to load it eagerly 这就是我试图尽快加载它的方式

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

Now, my problem is that the class ListViewControl get the correct records in both Actions and ListViewActions , but there are multiple entries of the same record. 现在,我的问题是类ListViewControlActionsListViewActions中都获取正确的记录,但是同一记录有多个条目。 The number of records is equal to the number of joins made to the ControlAction table, in this case two. 记录数等于对ControlAction表的联接数,在这种情况下为两个。

How can I avoid this? 如何避免这种情况? If I remove the SetFetchMode from the listViews query, the actions are loaded lazily through a proxy which I don't want. 如果我从listViews查询中删除SetFetchMode ,则通过不需要的代理延迟加载操作。

I also tried creating aliases ... 我也尝试创建别名...

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

This removed the duplicate entries, but did not eagerly load 这删除了重复的条目,但没有急于加载

That's an extremely inefficient way to eager load collections. 这是一种急于收集负载的极其低效的方法。 Here's a better way: 这是一个更好的方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM