使用带有 NHibernate.Mapping.Attributes 的鉴别器为每个子类做表

发布于 2024-09-07 15:08:18 字数 2223 浏览 6 评论 0原文

我正在尝试执行 每个子类的表:使用 NHibernate.Mapping.Attributes 的鉴别器。 hbm 应该如下所示:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <subclass name="SalesReport" discriminator-value="SalesReport" extends="Report">
    <join table="SalesReport">
      <key foreign-key="FK_SalesReport_Document" />
      <property name="TotalSales" />
      <property name="NetGrouth" />
    </join>
  </subclass>
</hibernate-mapping>

代码如下所示:

[Subclass(NameType=typeof(SalesReport),
    DiscriminatorValueObject=DocumentDiscriminator.SalesReport,
    ExtendsType=typeof(Report))]
[Serializable]
public partial class SalesReport : Common.Documents.Report
{
    [Join(Table = "SalesReport")]
    [Key(1, ForeignKey = "FK_SalesReport_Document")]
    [Property(2)]
    public virtual decimal TotalSales
    {
        get;
        set;
    }

    [Join(Table = "SalesReport")]
    [Key(1, ForeignKey = "FK_SalesReport_Document")]
    [Property(2)]
    public virtual decimal NetGrouth
    {
        get;
        set;
    }
}

结果如下所示:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <subclass name="SalesReport" discriminator-value="SalesReport" extends="Report">
    <join table="SalesReport">
      <key foreign-key="FK_SalesReport_Document" />
      <property name="TotalSales" />
    </join>
    <join table="SalesReport">
      <key foreign-key="FK_SalesReport_Document" />
      <property name="TotalSales" />
      <property name="NetGrouth" />
    </join>
  </subclass>
</hibernate-mapping>

它创建了正确的表结构(一个具有两个十进制字段的表和一个到报告表的 PF/FK),但是在插入数据时它会失败,因为它为每个字段生成两个插入:

NHibernate: INSERT INTO SalesReport (TotalSales, SalesReport) VALUES (@p0, @p1);@p0 = 13400,20, @p1 = 41
NHibernate: INSERT INTO SalesReport (NetGrouth, SalesReport) VALUES (@p0, @p1);@p0 = 0, @p1 = 41

有没有办法使用 NHibernate.Mapping.Attributes 来完成此操作?

I'm trying to do Table per subclass: using a discriminator using NHibernate.Mapping.Attributes.
The hbm should look like this:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <subclass name="SalesReport" discriminator-value="SalesReport" extends="Report">
    <join table="SalesReport">
      <key foreign-key="FK_SalesReport_Document" />
      <property name="TotalSales" />
      <property name="NetGrouth" />
    </join>
  </subclass>
</hibernate-mapping>

The code looks like this:

[Subclass(NameType=typeof(SalesReport),
    DiscriminatorValueObject=DocumentDiscriminator.SalesReport,
    ExtendsType=typeof(Report))]
[Serializable]
public partial class SalesReport : Common.Documents.Report
{
    [Join(Table = "SalesReport")]
    [Key(1, ForeignKey = "FK_SalesReport_Document")]
    [Property(2)]
    public virtual decimal TotalSales
    {
        get;
        set;
    }

    [Join(Table = "SalesReport")]
    [Key(1, ForeignKey = "FK_SalesReport_Document")]
    [Property(2)]
    public virtual decimal NetGrouth
    {
        get;
        set;
    }
}

The result is something like this:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <subclass name="SalesReport" discriminator-value="SalesReport" extends="Report">
    <join table="SalesReport">
      <key foreign-key="FK_SalesReport_Document" />
      <property name="TotalSales" />
    </join>
    <join table="SalesReport">
      <key foreign-key="FK_SalesReport_Document" />
      <property name="TotalSales" />
      <property name="NetGrouth" />
    </join>
  </subclass>
</hibernate-mapping>

It creates the right table structure (one table with two decimal fields and one PF/FK to Reports table) but when insterting data it failes, because it generetes two inserts for each field:

NHibernate: INSERT INTO SalesReport (TotalSales, SalesReport) VALUES (@p0, @p1);@p0 = 13400,20, @p1 = 41
NHibernate: INSERT INTO SalesReport (NetGrouth, SalesReport) VALUES (@p0, @p1);@p0 = 0, @p1 = 41

Is there a way to get this done using NHibernate.Mapping.Attributes?

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

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

发布评论

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

评论(1

只是偏爱你 2024-09-14 15:08:18

您应该遵循 XML 版本来使用 NHMA:
因此,如果您想要一个,只需添加一个 NHMA.Join,以及两个 [Property(2 & 3, Name="...")]

You should follow the XML version to use NHMA:
So, if you want one , only add one NHMA.Join, with two [Property(2 & 3, Name="...")]

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