在NHibernate中按区分词查询 [英] Query by discriminator in 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?