1
一些背景。我們創建了一個名爲SqlSum的類型。它有2個屬性,Id和SumValue。爲什麼我的第一個查詢的值從我的第二個查詢返回值?
我在同一會話兩個查詢:
SqlSum paidTemp = session.CreateSQLQuery(
"select count(p.id) as Id, Sum(p.PaymentAmount) as SumValue " +
"FROM StPayments p " +
"where p.Active = 1 and p.IsVoided = 0 and p.StCustomerFk = :custid")
.AddEntity(typeof(SqlSum))
.SetParameter("custid", cust.Id)
.List<SqlSum>().First();
if (paidTemp != null)
{
paid = paidTemp.SumValue;
}
SqlSum allocTemp = session.CreateSQLQuery(
"select count(pA.id) as Id, Sum(pA.Amount) As SumValue " +
"FROM StPaymentAllocations pA " +
"INNER JOIN StPayments p on pA.StPaymentFk = p.Id " +
"where pA.Active = 1 and p.StCustomerFk = :custid")
.AddEntity(typeof(SqlSum))
.SetParameter("custid", cust.Id)
.List<SqlSum>().First();
if (allocTemp != null)
{
allocated = allocTemp.SumValue;
}
我可以在分析器,用於paidtemp的查詢返回的1575一sumvalue和allocTemp查詢返回的1500的值清楚地看到,但是,付費變量和分配變量都被賦值爲1575.事實上,調試器中的allocTemp.SumValue屬性的檢查顯示值爲1575.
現在,我做了一些小的更改並將其中的每個查詢他們自己的會議:
using (var session = factory.OpenSession())
using (var trans = session.BeginTransaction(IsolationLevel.ReadCommitted))
{
SqlSum paidTemp = session.CreateSQLQuery(
"select count(p.id) as Id, Sum(p.PaymentAmount) as SumValue " +
"FROM StPayments p " +
"where p.Active = 1 and p.IsVoided = 0 and p.StCustomerFk = :custid")
.AddEntity(typeof(SqlSum))
.SetParameter("custid", cust.Id)
.List<SqlSum>().First();
if (paidTemp != null)
{
paid = paidTemp.SumValue;
}
trans.Commit();
session.Flush();
}
using (var session = factory.OpenSession())
using (var trans = session.BeginTransaction(IsolationLevel.ReadCommitted))
{
SqlSum allocTemp = session.CreateSQLQuery(
"select count(pA.id) as Id, Sum(pA.Amount) As SumValue " +
"FROM StPaymentAllocations pA " +
"INNER JOIN StPayments p on pA.StPaymentFk = p.Id " +
"where pA.Active = 1 and p.StCustomerFk = :custid")
.AddEntity(typeof(SqlSum))
.SetParameter("custid", cust.Id)
.List<SqlSum>().First();
if (allocTemp != null)
{
allocated = allocTemp.SumValue;
}
trans.Commit();
session.Flush();
}
執行此代碼時,突然alloctermp.SumValue是1500預期。
是什麼導致第二個查詢在第一個示例中保留來自第一個查詢的值?
這兩個計數(您定義爲Id)是否返回相同的值? – Rippo
是的!我認爲這解釋了一切,謝謝。 –
將發佈答案... – Rippo