tags:

views:

33

answers:

2

It's been asked a million times, its like this.

Say Invoice is the base class and InvoiceHistory is the class that simply inherits from Invoice.

When I do something like invoiceList = session.CreateCriteria(typeof(Invoice)).List();

I get everything from Invoice (that I want, plus everything from InvoiceHistory).

Do I need to have an InvoiceBase and create derived versions for Invoice and InvoiceHistory?

+2  A: 

I think this has to do with polymorphism in NHibernate. Try specifying polymorphism="explicit" on the mapping for your base-class (Invoice).

jishi
very nice. thanks
jeff
A: 

If you don't want to retrieve the invoicehistory for an invoice inheritance wouldn't do the trick. Even creating an InvoiceBase would not help. If you are using inheritance nhibernate will always return the most complex object that exists in database. So if there is a foreign key in the invoicehistory pointing to an invoice you will alway get the invoicehistory object instead of a simple invoice. This is a fundamental feature of nhibernate.
You could excplicitly fetch only the properties of invoice and map them by hand using a ResultTransformer (see Reference for more infos) or create and map a SimpleInvoice object also referencing the invoice table, but with the latter you may face some stales-state issues is you mix Invoice and SimpleInvoice within the same session. Hope this helps.

zoidbeck