Comparing ICompositeUserType objects in nHibernate
I've made POCO object mapped to database like this:
class MoneyObject
{
Money MoneyAmmount { get;set; }
string Description { get;set; }
}
MoneyAmmount - is type Money which is derived from ICompositeUserType and has 2 properties decimal Amount and string CurrencyCode:
class Money
{
decimal Ammount { get;set; }
string CurrencyCode { get;set; }
}
Problem appears when I'm trying to fetch data from database using criteria, like following:
var criteria = session.CreateCriteria(typeof(MoneyObject))
.Add(Expression.Lt("MoneyAmmount", money));
It generates following sql query:
SELECT this_.Id as Id2_0_, this_.Value as Value2_0_, this_.Currency as Currency2_0_, this_.Description as Descript4_2_0_
FROM MoneyObject this_
WHERE this_.Value < @p0 and this_.Currency &开发者_如何学Golt; @p1;@p0 = 300,01, @p1 = 'USD'
I don't want to compare my valuetype object Money using CurrecyCode by character. I want to compare Money only by amount property and fetch data using criteria. But dealing in criteria only with properties of POCO object. Means I know that comparision works:
Expression.Lt("MoneyAmmount.Ammount", money.Ammount)
I want to avoid comparison by valuetype object properties and achieve comparison only between valuetype objects like
Expression.Lt("MoneyAmmount", money)
Does anybody know how to change such comparing behavior?
Try something like this
var criteria = session.CreateCriteria(typeof(MoneyObject), "m")
.CreateAlias("m.MoneyAmmount", "a")
.Add(Expression.Lt("a.Ammount", money));
Creating the aliases "m" and "a" lets you compare to the nested property.
精彩评论