开发者

How do I map a derived property on a base class using NHibernate?

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 BusinessAs开发者_开发问答sessment
{
    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?


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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜