views:

60

answers:

3

How do I query a class of a specific entity in NHibernate?
I basically want a projection that returns a System.Type of each row that matches criteria.

I have looked at http://stackoverflow.com/questions/2787498/get-subclass-type-from-projection-with-nhibernate however when I create Projections.Property("alias.class") or Projections.Property("class"), I always get could not resolve property 'class'.

A: 

I don't think this is possible using NHibernate directly; but consider adding the following to your base entity class (assuming you have one):

protected virtual Type GetTypeUnproxied() {
    return GetType();
} 

After you have queried your entities, you can interrogate this property to return the actual CLR type of the entity.

DanP
My main idea is to skip a ton of fields that are in these entities and load only one property (additional Guid) and Type. This is a very specific scenario that can happen often, and entities are rather large because they must conform to rules of system that I am building a part of.
Andrey Shchekin
@Andrey: Perhaps you can map and return the discriminator column? You could then perform some conversion process to get the type based on this...
DanP
Unfortunately, I am building a framework where other people can add their own entities, some of which can have any kind of discriminators they want to (and other may have none). What I need is to know the entity Type so I can communicate it to the system. Right now I have to load full entity, which is imperfect.
Andrey Shchekin
@Andrey: Perhaps you need some sort of registry for the discriminators then? (let's assume it's alright for every class to have one); you could then map this as an entity and query on it as needed.
DanP
A: 

If you can't get access to the type through NHibernate for projection purposes, perhaps you can store the System.Type in a field using a custom user type. This should give you the exact functionality you require.

DanP
Yes, this is one solution. I would prefer not to extend database just for one query (even a common one). But I may take this route. Though it still leaves me in the dark on possibility to query the class without changing the database.
Andrey Shchekin
@Andrey - Agreed, it would be *nice* if NHibernate offered this functionality directly, but that doesn't appear to be the case. I'm guessing you'll need to adopt a convention-based solution along the lines of this to get the functionality that you're after.
DanP
Yes, but answers to the question I linked to tells that it supports "class" in some way, I was wondering if I can use some result transformer or post-processing to resolve "an integer, which is used internally".
Andrey Shchekin
@Andrey: This question might be better asked in the nhusers group - perhaps one of the nh devs can provide you with a better answer
DanP
A: 

Projections.Property("class") is possible and it works, but only if the class has a discriminator.

I got an answer from person on my team (Denis Bykov).
Unfortunately I had hard time making him answer here so I can award him reputation.

Andrey Shchekin