我有这些实体:
public class Root
{
/// <summary>
/// Contains reference to Base class, but my goal is to get only DerivedA in the query
/// </summary>
public Base Base { get; set; }
}
public class Base
{
public int Id { get; set; }
}
public class DerivedA: Base
{
public AnotherEntity Another { get; set; }
}
public class DerivedB : Base
{
public AnotherEntity Another { get; set; }
}
public class DerivedC : Base
{
}
public class AnotherEntity
{
}
我想从所有DerivedA实例中获取AnotherEntity。这是我的QueryOver:
var list = Session.QueryOver<Root>()
.JoinQueryOver(x => x.Base)
.JoinQueryOver(x => (x as DerivedA).Another)
.List();
但是我得到一个例外,因为NHibernate为另一个实体生成两个连接:一个用于DerivedA,一个用于DerivedB表。
同样在生成的sql中,我为所有3个派生类留下了外连接。
是否可以使单个查询不是多态的? 或者可能还有另一种方法可以阻止使用相同的别名创建多个连接?