如何使用 NHibernate 将派生属性映射到基类上?

发布于 2024-10-02 18:40:03 字数 2546 浏览 0 评论 0原文

我在某些映射方面遇到问题。假设我有三个评估,它们都源自基础 Assessment:换句话说

public abstract class Assessment
{
    public abstract int DamageCostTotal { get; set; }
}

public class IndividualAssessment
{
    public virtual int DamageCostHouse { get; set; }
    public virtual int DamageCostCar { get; set; }
    public virtual int DamageCostBelongings { get; set; }
    public override DamageCostTotal
    {
        get { return DamageCostHouse + DamageCostCar + DamageCostBelongings; }
    }
}

public class BusinessAssessment
{
    public virtual int DamageCostBuilding { get; set; }
    public virtual int DamageCostGoods { get; set; }
    public virtual int DamageCostOther { get; set; }
    public override DamageCostTotal
    {
        get { return DamageCostBuilding + DamageCostGoods + DamageCostOther; }
    }
}

public class InfrastructureAssessment
{
    public virtual int DamageCostStructure { get; set; }
    public virtual int DamageCostEstimatedRepair { get; set; }
    public override DamageCostTotal
    {
        get { return DamageCostStructure + DamageCostEstimatedRepair; }
    }
}

,三种评估类型:IndividualAssessmentBusinessAssessment 和 < code>InfrastructuralAssessment,都有特定的损坏成本,但它们都实现了基类的 DamageTotalCost 以获得一项评估的总损坏成本。

在我的 Fluent NHibernate 映射中,我为每个特定评估映射了每个 DamageCostTotal:

// individual assessment
Map(x => x.DamageCostTotal)
    .Access.Readonly()
    .Formula("DamageCostHouse + DamageCostCar + DamageCostBelongings");
// the other two are mapped the same way, just with a different formula

当我查询特定评估类型时,这非常有效:

Session.Query<IndividualAssessment>().OrderBy(x => x.DamageCostTotal);
Session.Query<BusinessAssessment>().OrderBy(x => x.DamageCostTotal);
Session.Query<InfrastructureAssessment>().OrderBy(x => x.DamageCostTotal);

但是当我尝试查询基本评估类型时:

Session.Query<Assessment>().OrderBy(x => x.DamageCostTotal);

它会生成错误的 SQL(为更容易阅读):

SELECT ...
    DamageCostHouse + DamageCostCar + DamageCostBelongings as formula0_0_,
    DamageCostBuilding + DamageCostGoods + DamageCostOther as formula1_0_,
    DamageCostStructure + DamageCostEstimatedRepair as formula2_0_,
    FROM [Assessment] this_
    ORDER BY DamageCostStructure + DamageCostEstimatedRepair

如您所见,它正确创建了公式,但是当它执行 ORDER BY 时,它仅按 InfrastructuralAssessment 属性而不是公式进行排序。有谁知道如何映射基本 DamageCostTotal 以便此查询返回正确的结果?

I'm having trouble with a certain mapping. Let's say I have three assessments that all derive off a base Assessment:

public abstract class Assessment
{
    public abstract int DamageCostTotal { get; set; }
}

public class IndividualAssessment
{
    public virtual int DamageCostHouse { get; set; }
    public virtual int DamageCostCar { get; set; }
    public virtual int DamageCostBelongings { get; set; }
    public override DamageCostTotal
    {
        get { return DamageCostHouse + DamageCostCar + DamageCostBelongings; }
    }
}

public class BusinessAssessment
{
    public virtual int DamageCostBuilding { get; set; }
    public virtual int DamageCostGoods { get; set; }
    public virtual int DamageCostOther { get; set; }
    public override DamageCostTotal
    {
        get { return DamageCostBuilding + DamageCostGoods + DamageCostOther; }
    }
}

public class InfrastructureAssessment
{
    public virtual int DamageCostStructure { get; set; }
    public virtual int DamageCostEstimatedRepair { get; set; }
    public override DamageCostTotal
    {
        get { return DamageCostStructure + DamageCostEstimatedRepair; }
    }
}

In other words, the three assessment types, IndividualAssessment, BusinessAssessment, and InfrastructureAssessment, all have specific damage costs, but they all implement the base class' DamageTotalCost in order to get one assessment's total damage cost.

In my Fluent NHibernate mappings, I mapped each DamageCostTotal for each specific assessment:

// individual assessment
Map(x => x.DamageCostTotal)
    .Access.Readonly()
    .Formula("DamageCostHouse + DamageCostCar + DamageCostBelongings");
// the other two are mapped the same way, just with a different formula

This works great when I query over the specific assessment types:

Session.Query<IndividualAssessment>().OrderBy(x => x.DamageCostTotal);
Session.Query<BusinessAssessment>().OrderBy(x => x.DamageCostTotal);
Session.Query<InfrastructureAssessment>().OrderBy(x => x.DamageCostTotal);

But when I try to query over the base assessment type:

Session.Query<Assessment>().OrderBy(x => x.DamageCostTotal);

It generates the wrong SQL (cleaned up a bit for easier readability):

SELECT ...
    DamageCostHouse + DamageCostCar + DamageCostBelongings as formula0_0_,
    DamageCostBuilding + DamageCostGoods + DamageCostOther as formula1_0_,
    DamageCostStructure + DamageCostEstimatedRepair as formula2_0_,
    FROM [Assessment] this_
    ORDER BY DamageCostStructure + DamageCostEstimatedRepair

As you can see, it's creating the formulas correctly, but when it does the ORDER BY, it only orders it by the InfrastructureAssessment properties and not the formulas. Does anyone know how to map the base DamageCostTotal so that this query will return the correct result?

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

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

发布评论

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

评论(1

孤单情人 2024-10-09 18:40:03

一种解决方案是将 DamageCostTotal 映射到基类中。如果表中没有 DamageCostTotal 列,则创建一列。 NHibernate 不需要了解这种情况下的公式,这很好,因为这些类已经在承担繁重的工作。

您的查询应该生成与此类似的 SQL...

SELECT ..., this_.DamageCostTotal FROM Assessment this_ ORDER BY this_.DamageCostTotal

One solution would be to map DamageCostTotal in the base class. Create a column for DamageCostTotal in the table if there isn't one. NHibernate won't need to know about the formulas in this scenario, which is fine since the classes are already doing the heavy lifting.

Your query should result in SQL similar to this...

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