views:

43

answers:

2

Suppose I have this relationship:

abstract class Base { int Id; int JoinedId; ... }
class Joined { int Id; int Discriminator; ... }
class Sub1 : Base { ... }
class Sub2 : Base { ... }

for the following tables:

table Base ( Id int, JoinedId int, ... )
table Joined ( Id int, Discriminator int, ... )

I would like to set up a table-per-hierarchy inheritance mapping for the Base, Sub1, Sub2 relationships, but using the Disciminator property from the Joined class as the discriminator.

Here's the general idea for the mapping file:

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

    <discriminator /> <!-- ??? or <join> or <many-to-one>? -->

    <subclass name="Sub1" discriminator-value="1">...</subclass>
    <subclass name="Sub2" discriminator-value="2">...</subclass>
</class>

Is there any way of accomplishing something like this with the <discriminator>, <join>, or <many-to-one>? NHiberante seems to assume the discriminator is a column on the given table (which makes sense to me.. I know this is unorthodox).

Thanks.

A: 

Short answer is: it can't be done, unless you use a view as the table.

I've had the same problem in my current project, and I had to work around it using a strategy pattern.

Diego Mijelshon
A: 

Can't you use a discriminator-formula with a subselect on Joined?

<discriminator formula="(select j.discriminator from Joined j where j.id = joinedid)">
meriton