开发者

nhibernate table per subclass strategy problem when getting objects

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 a开发者_JS百科bstract 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(link text):

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

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

  <joined-subclass
    name="Document1"
    extends="Document" >

    <key column="ParentId"/>
    ...
  </joined-subclass>

  <joined-subclass
    name="Document2"
    extends="Document" >

    <key column="ParentId"/>
    ...
  </joined-subclass>

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:

GetById<Document>(1)

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜