开发者

NHibernate - How to fetch conditional data from database using hbm file mapping

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开发者_如何学Go 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.


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

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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜