开发者

Doing table per subclass using a discriminator with NHibernate.Mapping.Attributes

开发者 https://www.devze.com 2023-01-04 22:17 出处:网络
I\'m trying to do Table per subclass: using a discriminator using NHibernate.Mapping.Attributes. The hbm should look like this:

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?


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="...")]

0

精彩评论

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