简体   繁体   English

为什么我的第一个查询的值作为第二个查询的值返回?

[英]Why are my values of my first query, being returned as values from my second query?

Some background. 一些背景。 We created a type called SqlSum. 我们创建了一个名为SqlSum的类型。 It has 2 properties, Id and SumValue. 它具有2个属性,Id和SumValue。

I have two queries in the same session: 我在同一会话中有两个查询:

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;
}

I can clearly see in the profiler that the query for paidtemp is returning a sumvalue of 1575 and the allocTemp query is returning a value of 1500, however, both the paid and allocated variables are assigned the value of 1575. In fact inspection of the allocTemp.SumValue property in the debugger shows a value of 1575. 我可以在探查器中清楚地看到,针对paytemp的查询返回的总和为1575,而allocTemp查询的返回值为1500,但是,已支付和已分配变量均被分配为1575。实际上,对allocTemp的检查调试器中的.SumValue属性显示值为1575。

Now, I made some minor changes and moved each of these queries into their own sessions: 现在,我做了一些细微的更改,并将每个查询移至各自的会话中:

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();
}

upon execution of this code, suddenly the alloctemp.SumValue is 1500 as expected. 执行此代码后,突然alloctemp.SumValue为1500。

What was causing the second query to keep the values from the first query in my first example? 是什么导致第二个查询保留第一个示例中第一个查询的值?

If your Id column values can return the same value then the first level cache already thinks this id is in it and will duplicate row. 如果您的Id列值可以返回相同的值,则一级缓存已经认为该ID在其中,并且将duplicate行。

Therefore:- 因此:-

  1. If you want to keep the Id as a column then transform into a dto remembering to override Equals and GetHashCode . 如果您想将Id保留为一列,则请转换为dto记住要重写EqualsGetHashCode eg see this exmple 例如看这个例子
  2. Don't use the column name Id (too be honest I am not sure about this one). 不要使用列名Id (老实说,我不确定这一点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM