NHibernate:使查询不是多态的

时间:2017-01-13 16:53:24

标签: join nhibernate queryover

我有这些实体:

    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个派生类留下了外连接。

是否可以使单个查询不是多态的? 或者可能还有另一种方法可以阻止使用相同的别名创建多个连接?

0 个答案:

没有答案