NHibernate discriminator-value =" null"不在基类上工作 - 未应用过滤器

时间:2012-06-06 17:17:40

标签: nhibernate

用于以下类映射:

<class name="Person" table="Person" discriminator-value="null">
    <id name="ID" column="ID" >
        <generator class="identity"/>
    </id>
    <discriminator column="MasterId" />
    <property name="LongName" column="LONGNAME" />
    <property name="ShortName" column="SHORTNAME" />
    // other stuff here ...
    <subclass name="PersonHistory" discriminator-value="not null">
        <property name="MasterId" />
    </subclass>
</class>

执行时:

        var query =
            from lac in session.Query<Person>()
            orderby lac.LongName
            select lac;
        return query.ToList();

我从 Person 表中获取所有条目,两者都将MasterId设置为null而不是null。有没有办法让NHibernate只获取MasterId = null的实体?

2 个答案:

答案 0 :(得分:1)

如何在您的案例中使用discriminator公式?

<discriminator formula="case when MasterId is null then 0 else 1 end" />

然后为类设置了discriminator属性值:

  • For Person:discriminator-value =&#34; 0&#34;
  • For PersonH​​istory:discriminator-value =&#34; 1&#34;

答案 1 :(得分:1)

为了实现我想要的,我创建了基类+两个子类。这是配置:

  • discriminator-value 的子类:

    <subclass name="People" discriminator-value="null">
    </subclass>
    <subclass name="PeopleHistory" discriminator-value="not null">
        <property name="MasterRowId" />
    </subclass>
    
  • 基础中的鉴别器:

    <discriminator column="MasterRowId" />