NHibernate 通过外键将子类属性映射到另一个对象

发布于 2024-11-05 20:34:49 字数 1337 浏览 0 评论 0原文

我正在研究一个不完美的现有数据结构,并且我有一个继承映射问题需要解决。

我在每个层次结构中使用一个表,并设置了带有鉴别器的子类。然而,子类属性是返回其他表的外键。如何设置子类映射,以便在查询 fk 属性时得到一个对象而不是 null?这可能吗?

我当前的映射

<?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 name="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>

我想添加一个实体图,但我还不能发布图像:0(

但是外键 AupairId 和 FamilyId 链接回另外两个表,并且类型为 int 32

我知道最好重组 Aupair 并且家庭实体有一个层次结构来摆脱图像中的层次结构,但由于现有代码,这将是最后的手段,

在此先感谢您的地图专家提供的任何帮助......

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 name="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....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

或十年 2024-11-12 20:34:49

再看一眼后意识到我做错了什么,应该在子类下使用具有定义列的多对一

    <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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文