如何使用 NHibernate 将派生属性映射到基类上?
我在某些映射方面遇到问题。假设我有三个评估,它们都源自基础 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; }
}
}
,三种评估类型:IndividualAssessment
、BusinessAssessment
和 < 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种解决方案是将 DamageCostTotal 映射到基类中。如果表中没有 DamageCostTotal 列,则创建一列。 NHibernate 不需要了解这种情况下的公式,这很好,因为这些类已经在承担繁重的工作。
您的查询应该生成与此类似的 SQL...
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...