开发者

NHibernate Mapping a sublass property to another object via foreign key

I am working on an existing data structure that is not perfect and I have an inheritance mapping issue to solve.

I am using a table per hierarchy and have subclasses with discriminators set up. However the subclassed properties are foreign keys back to other tables. How do I set up my subclass mapping so that when I query the fk property I get an object rather than null? Is this even possible?

My current Mapping

<?xml version="1.0" encoding="utf-8" ?>
      <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MVC3" namespace="MVC3.Models">
  <class name="Image" table="Images">
<id name="Id" column="ImageId">
  <generator class="identity" />
</id>
<discriminator column="ImageType" />
<property name="Url" column="Url" not-null="true" />
<property name="Caption" column="Caption" />
<subclass name="AupairImage" discriminator-value="AupairImage">
  <join table="Aupairs" inverse="true">
    <key column="AupairId" />
    <many-to-one na开发者_如何学Gome="Aupair" column="AupairId" class="Aupair" />
  </join>
</subclass>
<subclass name="FamilyImage" discriminator-value="FamilyImage">
  <join table="Families" inverse="true">
    <key column="FamilyId" />
    <many-to-one name="Family" column="FamilyId" class="Family" />
  </join>      
</subclass>

I would have like to add an entity diagram but I cannot post images yet :0(

But the foreign keys AupairId and FamilyId link off back to two other tables and are of the type int 32

I know it would be better to restructure the Aupair And Family entities to have a hierarchy to get rid of the one in images but this would be a last resort due to existing code.

Thanks in advance any help appreciated from you mapping experts....


Realised what I was doing wrong after taking a second look, should have just used a Many-To-One under the Subclass with a defined column

    <subclass name="MVC3.Models.FamilyImage, MVC3" discriminator-value="FamilyImage">
     <many-to-one cascade="all" class="MVC3.Models.Family, MVC3" name="Family">
        <column name="FamilyId" />
      </many-to-one>
    </subclass>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜