Tabla nhibernada por problema de estrategia de subclase al obtener objetos

I am working on an ASP.NET Web application that uses NHibernate and I have noticed something funny.
So I have an object Document which I would make into an abstract class and 2 concrete implementations Document1 and Document2. I tried to write the mappings for them applying the table-per-subclass strategy as described in the documentation(texto del enlace):

    <class name="Document" abstract="true">

        <id name="Id">
          <generator class="identity"/>

    extends="Document" >

    <key column="ParentId"/>

    extends="Document" >

    <key column="ParentId"/>

Now this is how I obtain objects from the session in my application:

public TEntity GetById<TEntity>(object id) {
    return Session.Get<TEntity>(id);

Now my problem is that when I do:


for example, I don't get a Document object I get an object of type Document1 or Document2 depending on which type that object is.

I tryed using the table-per-subclass with discriminators strategy as mentioned in the documentation(link above) and I set join=select and lazy=false on the abstract object to get it to return an object of type Document but nothing worked.

The code works but it doesn't seem right. I have a left join where I could not use one. Isn't there a way to just get the abstract object or does nhibernate actually instantiate the objects it returns which would make this impossible? Is it possible?

I have the feeling that I am getting more info than I need.

preguntado el 08 de enero de 11 a las 17:01

2 Respuestas

You'll never get the abstract class because the whole point of an abstract class is that you can't instantiate one. Besides your code will return only the Document facade to the rest of your code anyway. So I'm not sure what the problem really is.

Respondido el 08 de enero de 11 a las 20:01

I know you cannot instantiate an abstract class but you can cast an object to the abstract class's type. - Para

@Para, that's true, but if you examine the object it will still be the concrete type. - Vadim

It is not possible to retrieve an instance of Document1 or Document2 that only populates the properties that are defined in the abstract class. In .NET, you are always working with an instance of a concrete class even if the type is declared as an interface or abstract class.

Respondido el 09 de enero de 11 a las 17:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.