c# - How do I map a derived property on a base class using NHibernate? -
i'm having trouble mapping. let's have 3 assessments derive off 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 { { 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 { { return damagecostbuilding + damagecostgoods + damagecostother; } } } public class infrastructureassessment { public virtual int damagecoststructure { get; set; } public virtual int damagecostestimatedrepair { get; set; } public override damagecosttotal { { return damagecoststructure + damagecostestimatedrepair; } } }
in other words, 3 assessment types, individualassessment
, businessassessment
, , infrastructureassessment
, have specific damage costs, implement base class' damagetotalcost
in order 1 assessment's total damage cost.
in fluent nhibernate mappings, mapped each damagecosttotal each specific assessment:
// individual assessment map(x => x.damagecosttotal) .access.readonly() .formula("damagecosthouse + damagecostcar + damagecostbelongings"); // other 2 mapped same way, different formula
this works great when query on 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 try query on base assessment type:
session.query<assessment>().orderby(x => x.damagecosttotal);
it generates wrong sql (cleaned bit easier readability):
select ... damagecosthouse + damagecostcar + damagecostbelongings formula0_0_, damagecostbuilding + damagecostgoods + damagecostother formula1_0_, damagecoststructure + damagecostestimatedrepair formula2_0_, [assessment] this_ order damagecoststructure + damagecostestimatedrepair
as can see, it's creating formulas correctly, when order by
, orders infrastructureassessment
properties , not formulas. know how map base damagecosttotal
query return correct result?
one solution map damagecosttotal in base class. create column damagecosttotal in table if there isn't one. nhibernate won't need know formulas in scenario, fine since classes doing heavy lifting.
your query should result in sql similar this...
select ..., this_.damagecosttotal assessment this_ order this_.damagecosttotal
Comments
Post a Comment