Hello!
I'm using a named query to get statistical data. If the parameter avgMonth changes, there will be different values for the column Average. As far as I can see with Profiler, the query is properly sent to SQL-Server. Unfortunately NHibernate returns a collection with unchanged values. I tried all settings to disable caching, but no changes. Any suggestions?
The mapping file:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Finance.Core" namespace="REF.Finance.Core">
<!-- the class definition -->
<class name="BookingStatistic">
<cache usage="read-only" />
<composite-id>
<key-many-to-one name="Account" column="Account" class="Account" />
<key-property name="Date" column="Date" type="DateTime" />
</composite-id>
<property name="Amount" column="Amount" type="float" />
<property name="Average" column="Average" type="float" />
</class>
<!-- query daily statistic values -->
<sql-query name="BookingStatistic.Daily" cacheable="false" cache-mode="ignore" read-only="true" flush-mode="always">
<return class="BookingStatistic" />
<![CDATA[
with source as
(
select bkg_acc_id,
cast(cast(year(bkg_date) as nvarchar) + right('0' + cast(month(bkg_date) as nvarchar), 2) + '01' as datetime) as bkg_date,
bkg_amount
from booking
where bkg_acc_id = :account
and bkg_date >= dateadd(month, -:avgMonth, :fromDate)
and bkg_date <= :toDate
),
aggregate as
(
select bkg_acc_id,
bkg_date,
sum(bkg_amount) as bkg_amount
from source
group by bkg_acc_id,
bkg_date
),
movingAverage as
(
select bk1.bkg_acc_id,
bk1.bkg_date,
avg(bk2.bkg_amount) as Average
from aggregate as bk1
inner join aggregate as bk2
on bk2.bkg_acc_id = bk1.bkg_acc_id
and bk2.bkg_date >= dateadd(month, -:avgMonth, bk1.bkg_date)
and bk2.bkg_date <= bk1.bkg_date
group by bk1.bkg_acc_id,
bk1.bkg_date
)
select bk1.bkg_acc_id as Account,
bk1.bkg_date as Date,
bk1.bkg_amount as Amount,
bk2.Average as Average
from aggregate as bk1
inner join movingAverage as bk2
on bk2.bkg_acc_id = bk1.bkg_acc_id
and bk2.bkg_date = bk1.bkg_date
where bk1.bkg_date >= :fromDate
and bk1.bkg_date <= :toDate
order by bk1.bkg_date
]]>
</sql-query>
</hibernate-mapping>
The loading function:
Code:
public void Load(Account account, DateTime fromDate, DateTime toDate, int avgMonth)
{
IQuery query = Database.OpenSession().GetNamedQuery("BookingStatistic.Daily");
query.SetParameter("account", account);
query.SetInt32("avgMonth", avgMonth);
query.SetDateTime("fromDate", fromDate);
query.SetDateTime("toDate", toDate);
m_Collection = query.List<BookingStatistic>();
OnListChanged(ListChangedType.Reset, -1);
}
Greetings!
Zorgoban