简体   繁体   English

Session.CreateSqlQuery忽略Session.Update之后的更新值

[英]Session.CreateSqlQuery ignoring updated values after a Session.Update

Problem 问题

I have a Session.Update() updating the status of a row. 我有一个Session.Update()更新行的状态。 The next line of code is a Session.CreateSqlQuery() with a more complex update query. 下一行代码是带有更复杂的更新查询的Session.CreateSqlQuery() They are both inside a transaction, as follows: 它们都在事务内,如下所示:

private void RemoveReferences(Person obj)
{
    //Session is ISession
    //Person contains multiple Activity, that can contains multiple DetailedActivity.

    using (var tx = Session.BeginTransaction())
    {
        foreach (var act in obj.Activities)
        {
            var det = Session.QueryOver<DetailedActivity>()
                .Where(w => w.Activity.CdActivity == act.CdActivity)
                .Take(1).SingleOrDefault();

            //Null validation removed...

            det.Date = null;
            det.Activity.Stage = 2; //Example of value being updated.
            Session.Update(det.Activity);
            Session.Update(det);
         }

         Session.Flush();

         //More code removed...

         //The next code is blind to any updated value from the code above.
         //For example, the value Stage still keeps its old value 
         //if I try to get if from a CreateSqlQuery.

         //Tries to remove the Person reference from the Activity.
         Session.CreateSQLQuery("Update activity ac " +
                                "Set cd_person = null " +
                                "From det_activity da " +
                                "Where ac.cd_activity = da.cd_activity " +
                                "And ac.cd_person = :cdPerson " +
                                "And Coalesce(ac.stage, 1) = 2 " +
                                "And da.Date is null;")
        .SetParameter("cdPerson", obj.CdPerson).ExecuteUpdate();

        tx.Commit();

        //Try/catch removed for brevity;
    }

This code is a simplified version of what I'm really working with, but the two basic blocks of code are there. 这段代码是我真正使用的代码的简化版本,但是其中有两个基本代码块。

For a CreateSqlQuery call, the value updated by a previous Session.Update() is not being "seen". 对于CreateSqlQuery调用,不会“看到”先前的Session.Update()更新的值。 I tried to get that row of activity using the CreateSqlQuery and it still returns the old value. 我试图使用CreateSqlQuery来获取该行activity ,但它仍返回旧值。

Questions 问题

Why is the CreateSqlQuery not seeing the updated value? 为什么CreateSqlQuery没有看到更新的值?

Is there any way to make the CreateSqlQuery see the updated value? 有什么方法可以使CreateSqlQuery看到更新后的值?

If I read that correctly, you seem to have misunderstood what "Update()" does. 如果我没看错的话,您似乎误解了“ Update()”的作用。 You may want to refer to the reference documentation: http://nhibernate.info/doc/nhibernate-reference/manipulatingdata.html#manipulatingdata-updating 您可能需要参考参考文档: http : //nhibernate.info/doc/nhibernate-reference/manipulatingdata.html#manipulatingdata-updating

When you are working with an instance that you just loaded and want to update it inside the same session , as you seem to be doing here, it's enough to just modify the object. 当您使用刚刚加载的实例并想在同一会话中更新实例时,就像您在此处所做的那样,只需修改对象即可。 The change will be sent to the database during NHibernate's dirty check and flush stage. 更改将在NHibernate的脏检查和刷新阶段发送到数据库。

Save(), Update() and SaveOrUpdate() are used for new objects, or for objects loaded in a previous session that you wish to attach to a new session. Save(),Update()和SaveOrUpdate()用于新对象,或用于您希望附加到新会话的先前会话中加载的对象。 They tell the NHibernate session to "please register this object for a later dirty check" (during some scenarios Save() may trigger immediate INSERT for technical reasons but that is a side-effect and not a promise). 他们告诉NHibernate会话“请注册该对象以供以后进行脏检查”(在某些情况下,Save()出于技术原因可能会立即触发INSERT,但这是副作用,而不是承诺)。

However, Update() should do no harm (it should be a no-op), and you do also call Flush() explicitly which should transmit your modifications to the database. 但是,Update()应该没有害处(应该是无操作),并且您也确实要显式调用Flush(),它应该将所做的修改传输到数据库。

So the explanation must lie elsewhere... 因此,解释必须位于其他地方...

What exactly is Session in this code? 这段代码中的Session到底是什么? Are you sure it's the exact same session instance throughout? 您确定它是完全相同的会话实例吗?

How are the classes mapped? 类如何映射? Anything with regards to flush rules, read-only properties etc? 关于刷新规则,只读属性等问题吗?

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

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