DiscriminateSubClasses question

79 views
Skip to first unread message

Roger Heim

unread,
Mar 5, 2009, 9:21:54 PM3/5/09
to Fluent NHibernate
Hi All,

I've just started using FNH and NHibernate. Between FNH and Steve
Bohlen's Summer of NHibernate videos I think I'm starting to get my
brain wrapped around this stuff but I have a question about using
discriminators and sub-classes.

I have an entity that implements a General Ledger account. The account
can be one of 5 types: Asset, Liability, Equity, Income or Expense and
is represented by an Int32 that has the value 1-5. I created a
GLAccount class that contains all the properties. The class looks like
this:
public class GLAccount
{
public virtual Guid AccountID { get; private set;}
public virtual decimal AccountNumber {get; set;}
public virtual string Description {get;set;}
public virtual int AccountType {get;set;}
public virtual int Version {get; private set;}
}

I also created five classes for the different account types:
GLAssetAccount, GLLiabilityAccount, GLEquityAccount, GLIncomeAccount
and GLExpenseAccount. They are all subclasses of GLAccount but they
have no additional properties. Here's the discriminator portion of the
mapping class:
DiscriminatorSubClassesOnColumn<int>("AccountType"), -1)
.SubClass<GLAssetAccount>(1), m=>m.Map
(x=>x.AccountType).Not.Nullable())
.SubClass<GLLiabilityAccount>(2), m=>m.Map
(x=>x.AccountType).Not.Nullable())
.SubClass<GLEquityAccount>(3), m=>m.Map
(x=>x.AccountType).Not.Nullable())
.SubClass<GLIncomeAccount>(4), m=>m.Map
(x=>x.AccountType).Not.Nullable())
.SubClass<GLExpenseAccount>(5), m=>m.Map
(x=>x.AccountType).Not.Nullable());

In my unit test I try to add an instance of the GLAccount object and
it passes but it assigns -1 as the AccountType even if I specifically
set it; I assume that's because of the -1 in the
DiscriminatorSubClassesOnColumn line. But if I try to add an instance
of a GLAssetAccount object it fails and throws an
IndexOutOfRangeException: Invalid index 4 for this
SqlParameterCollection with Count=4 and I never get to see the
generated SQL.

Based on my objects is using Table-Per-Class-Hierarchy correct? Can
anyone tell me what I've done wrong?

Roger Heim

James Gregory

unread,
Mar 6, 2009, 3:44:55 AM3/6/09
to fluent-n...@googlegroups.com
You don't normally have the discriminator as an actual property in your entity too, it's usually just a column. That's not to say it's incorrect, just something I've not seen before. Perhaps try it without.

Roger Heim

unread,
Mar 6, 2009, 2:42:02 PM3/6/09
to Fluent NHibernate
Thanks for the info James. Removing the discriminator as an actual
property helped but now I've run into issue #95 (where the
discriminator and the version tag are in the wrong sequence.) I tried
three times to attach an example to the issue that replicates the
problem but each time Google barfed. Briefly, it's dependent on the
order of the DiscriminateSubClassesOnColumn() and Version() in the
ClassMap file. If the sequence is DiscriminateSubClassesOnColumn()
followed by Version(), Fluently.Configure() succeeds but a generated
hbm.xml file is invalid. If the sequence is Version() followed by
DiscriminateSubClassesOnColumn(), Fluently.Configure() failes but a
generated hbm.xml file is correct.

I'm sorry I don't know enough about the FNH source to offer a patch.

On Mar 6, 3:44 am, James Gregory <jagregory....@gmail.com> wrote:
> You don't normally have the discriminator as an actual property in your
> entity too, it's usually just a column. That's not to say it's incorrect,
> just something I've not seen before. Perhaps try it without.
>

James Gregory

unread,
Mar 6, 2009, 3:07:26 PM3/6/09
to fluent-n...@googlegroups.com
Thanks for the info, this one has been a bit of a weird issue for a while now. If you want to try sending me the example (to this address) then I'll be grateful, but I can probably knock one together from what you've said.

Roger Heim

unread,
Mar 6, 2009, 3:24:59 PM3/6/09
to Fluent NHibernate
I'll the post the full project (it's probably bigger than Google's
limit) on my blog tonight then update this thread with a link.
> > > > Roger Heim- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages