使用带有 NHibernate.Mapping.Attributes 的鉴别器为每个子类做表
我正在尝试执行 每个子类的表:使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该遵循 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="...")]