Hallo!
Ich verwend eine benannte Abfrage für statistische Werte. Wenn sich der Parameter avgMonth ändert, ändern sich die Werte in der Spalte Average. Oder zumindest sollten sie das. NHibernate sendet, das hab ich mit Profiler nachgewiesen, die Abfrage an den SQL-Server. Die direkte Überprüfung ergibt auch, daß SQL-Server unterschiedliche Werte zurück liefert. Leider gibt mir NHibernate unveränderte Werte, als würde irgend ein Zwischenspeicher verwendet, welchen ich mit allen erdenklichen Mitteln zu unterdrücken versuche. Was übersehe ich hier?
Hier mal das Mapping:
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>
Dann noch die Lade-Funktion:
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);
}
Grüße!
Zorgoban