1

首先,一些代码来解释我的配置:

public class OrdersSessionFactoryCreator
    : ISessionFactoryCreator
{
    public NHibernate.ISessionFactory Create()
    {
        var ret =
            Fluently
                .Configure()
                .Database(MsSqlConfiguration
                    .MsSql2008
                    .DoNot.UseOuterJoin()
                    .UseReflectionOptimizer()
                    .ShowSql()
                    .FormatSql()
                    .ConnectionString(ex =>
                        ex.FromConnectionStringWithKey("OrdersSqlServer")))
                .Mappings(
                    x =>
                        x.AutoMappings.Add(
                            AutoMap.AssemblyOf<Order>(new AutoConfig()).UseOverridesFromAssemblyOf<Order>()
                                .Conventions.Add<DefaultStringLengthConvention>()
                                .Conventions.Add(DefaultCascade.None())))
                .ExposeConfiguration(config => new SchemaExport(config).Create(true, true))
                .BuildSessionFactory();

        return ret;
    }
}

现在,域模型:

public class Order : EntityBase
{
    public Order(Guid id)
        : base(id)
    {
    }

    protected Order()
    {
    }

    public virtual Customer Customer { get; set; }
}

public class Customer : EntityBase
{
    public Customer(Guid id)
        : base(id)
    {
    }
    protected Customer()
    {
    }

    public virtual IList<Order> Orders { get; set; }
}

public class OrderMap : IAutoMappingOverride<Order>
{
    public void Override(AutoMapping<Order> mapping)
    {
        //mapping.HasOne(c => c.Customer).Not.LazyLoad().Cascade.All();
        mapping.References(c => c.Customer).Not.Nullable();
    }
}

public class CustomerMap : IAutoMappingOverride<Customer>
{
    public void Override(AutoMapping<Customer> mapping)
    {
        mapping.HasMany(o => o.Orders).Cascade.All().Fetch.Join();
    }
}

现在,如果我进行这样的查询:

var orders = session.Query<Customer>().SelectMany(c => c.Orders).ToArray();

我对我的客户有一个适当的内部连接。但如果我从另一边做同样的事情:

var or = session.Query<Order>().Select(c => c.Customer).ToArray();

我得到了一个LEFT JOIN可以在大数据上造成严重性能下降的问题。

问题是:有没有办法指示 NHINNER JOIN用于我所有不可为空的引用列,所以我不会被迫使用QueryOver

4

2 回答 2

0

你尝试过真正的加入吗

var ordersWithCustomers = 
   from o in Session.Query<Order>()
   join c in Session.Query<Customer>() on o.Customer.Id equals c.Id
   select o;

return ordersWithCustomers.ToArray();

(有趣的是,LINQ to NH 从未支持左连接,QueryOver 摇滚恕我直言)。

于 2013-06-14T14:54:10.680 回答
0

您可以在 OrderMap 中尝试 Cascade.All() 吗:IAutoMappingOverride

于 2013-06-17T10:13:22.730 回答