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?
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"/>
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.
I tried 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>();
}
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…