NHibernate:通过区分器查询关联 [英] NHibernate: Query by discriminator on association

查看:185
本文介绍了NHibernate:通过区分器查询关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于此问题. 但是我想查询与一对一关系相关联的子实体的标识符,而又不知道确切的标识符值,即按类型而不是按字符串查询.

My question is similar to this question. But I want to query by the discriminator of a child entity associated with a one-to-one relationship, and without knowing the exact discriminator value, i.e., by type not by string.

给出一个类似的hbm:

Given an hbm like:

<class name="Parent" table="ParentTable">
    <id name="Id">
        <generator class="guid.comb" />
    </id>
    <one-to-one name="Child" class="IChild" property-ref="Parent" cascade="all" />
</class>

<class name="IChild" table="ChildTable" abstract="true">
    <id name="Id">
        <generator class="foreign">
            <param name="property">Parent</param>
        </generator>
    </id>   
    <discriminator column="TypeKey" type="String"/>
    <one-to-one name="Parent" class="Parent" />
</class>

<subclass name="ConcreteChild" extends="IChild" discriminator-value="Concrete1">
  <property name="SomeProperty"/>
</subclass>

或其他任何一对一配置,我想运行与此类似的查询:

Or any other one-to-one configuration, I would like to run a query similar to this:

public IEnumerable<Parent> FindByChild(Type childType)
{
    return session.CreateCriteria<Parent>()
        .Add(Restrictions.Eq("Child.class", childType))
        .List<Parent>();
}

更多信息:

  • 上述条件查询失败,因为"Child.class"未被识别为有效.

  • The above criteria query fails because "Child.class" is not recognized as valid.

使用HQL进行的类似查询失败,因为NHibernate在查询中使用childType的全名而不是其鉴别符值.

A similar query using HQL fails because NHibernate uses the fullname of childType rather than its discriminator value in the query.

推荐答案

您是否尝试过使用别名?我现在无法在我的计算机上对其进行测试,但应该与此类似.

Have you tried using an alias? I can't test it right now on my machine, but should be similar to this..

return session.CreateCriteria<Parent>()
                .CreateAlias("ParentChild","Child")
                .Add(Restrictions.Eq("ParentChild.class", childType))
                .List<Parent>();

其中ParentChild只是我用来引用Parent类的Child实体的名称

where ParentChild is just the name I used to refer to the Child entity of the Parent class

这篇关于NHibernate:通过区分器查询关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆