在NHibernate中按区分词查询 [英] Query by discriminator in NHibernate

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

问题描述

我对此进行了搜索,但没有发现任何问题.是否可以创建一个Hibernate查询以基于鉴别符返回一组对象?

I did some searching on this and didn't turn up anything. Is it possible to create a Hibernate query to return a set of objects based on a discriminator?

我有一个AbstractUser类,它由具体的类UserTypeA和UserTypeB扩展.我正在使用每个层次的表模型在NHibernate中映射我的类,因此UserTypeA和UserTypeB都存储在同一表中,具有不同的鉴别符值.这是我的鉴别符映射属性:

I have an AbstractUser class which is extended by the concrete classes UserTypeA and UserTypeB. I'm using the table-per-hierarchy model to map my classes in NHibernate, so UserTypeA and UserTypeB are both stored in the same table with different discriminator values. Here is my discriminator mapping property:

<discriminator column="Type" type="string"/>

我的表中有一列,其中包含用户类型的名称.我想知道是否可以使用此方法运行NHibernate查询.

I have a column in my table that contains the name of the user type. I'm wondering if it's possible to run a NHibernate query using this.

我尝试过:

public IList<DomainBase> FindByType(string typeName, Type type)
{
    string query = "from " + type.Name + " k where k.Type = " + typeName;
    return Session.CreateQuery(query).List<DomainBase>();
}

但是由于Type实际上不是类的属性,仅是表中的一列,所以这显然是行不通的.除非有一种使用属性作为鉴别符的方法,否则要同时拥有一个属性和一个鉴别符似乎是多余的?

But since Type is not actually a property of the class, just a column in the table, this obviously doesn't work. It would seem redundant to have both a property for this purpose and a discriminator, unless there's a way to use a property as a discriminator?

推荐答案

实际上,

同样,特殊属性class 访问 多态情况下的实例 坚持不懈. .Net类名称 嵌入在where子句中 转换为其鉴别值.

Likewise, the special property class accesses the discriminator value of an instance in the case of polymorphic persistence. A .Net class name embedded in the where clause will be translated to its discriminator value.

 from Eg.Cat cat where cat.class = Eg.DomesticCat

您还可以将System.Type实例作为参数传递.

You can also pass a System.Type instance as a parameter.

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

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