views:

22

answers:

2

Hello,

Currently I am using many-to-one element in hbm file to fetch the data object from database like following....

<property name="ContactId" length="4" />
<many-to-one
    name="DefaultContact"
    column="ContactId"
    class="Models.Contact"
    update="false"
    insert="false"/>

This code is fetching the data properly, but now I need to fetch the data conditionally like I am having the following properties and mant-to-one element in hbm file.....

<property name="ParentId" length="4" />
<property name="ParentType" length="4" />
<many-to-one
        name="ContactParent"
        column="???????? ParentId which could be CustomerId or ProspectId or LeadId according to Parent Type ????????"
        class="???????? Models.Customer or Models.Prospect or Models.Lead - according to Parent Type ????????"
        update="false"
        insert="false"/>

And I have to fetch the data according to value in "Parent Type" property, which means I need to set class attribute of "many-to-one" element dynamically according to "Parent Type" property.

So now, how can I achieve the desired result with many-to-one element or some other way...?

Thanks in Advance.

A: 

I can think of two ways to handle this.

  1. Don't include ContactParent in the model, use a separate method to retrieve it from ParentType and ParentId.

  2. Map all three parent types as private members and return the one that's not null in a public property.

Option 1 would be my first choice.

Jamie Ide
A: 

You should look into the <any> mapping http://nhforge.org/doc/nh/en/index.html#mapping-types-anymapping

It's the closest to what you want to do that NHibernate offers.

Diego Mijelshon