首先,一些代码来解释我的配置:
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
?