NHibernate does not pick up changes

I am wondering under what circumstances the following NHibernate code may crash:

var session = NHibernateSessionManager.CurrentSession;

var foo = session.Linq<Foo>.ToList()[0];

foo.SomeProperty = "test";

session.SaveOrUpdate(foo);

var reloadedFoos = session.Linq<Foo>
                         .Where(x => x.SomeProperty == "test");

Assert.That(reloadedFoos.Count > 0);

The Assert statement always fails.

If I manually call session.Flush after SaveOrUpdate, the selection request will succeed, however, I thought we did not need to manually call flush? I realized that NHibernate must be smart enough to understand that Foo has been updated, so the second select request must be successful.

Watching the generated SQL, a second SQL query appears, which is executed before the first SaveOrUpdate SQL code.

In fact, if I complete the entire method in a transaction, it will succeed:

using(NHibernateSessionManager.CurrentSession.BeginTransaction()
{
    // Same code as above
}

Now sql SaveOrUpdate will be executed before linq.Where sql. This is a little strange, since I donโ€™t even need to make a transaction between them.

What's happening?

+3
6

NHibernate. , NHibernate , SaveOrUpdate.

, . . http://nhprof.com/Learn/Alert?name=DoNotUseImplicitTransactions.

:

using(var session = NHibernateSessionManager.CurrentSession)
{
  using(var transaction = session.BeginTransaction())
  {
    var foo = session.Linq<Foo>.ToList()[0];

    foo.SomeProperty = "test";

    session.SaveOrUpdate(foo);
    transaction.Commit();
  }
}
+3

, NHibernate "smart".

:

var session = NHibernateSessionManager.CurrentSession;
using(NHibernateSessionManager.CurrentSession.BeginTransaction()) {
    var foo = session.Linq<Foo>.ToList()[0];
    foo.SomeProperty = "test";
    var reloadedFoos = session.Linq<Foo>()
        .Where(x => x.SomeProperty == "test");
    Assert.That(reloadedFoos.Count > 0);
}

, Save, Update SaveOrUpdate, , , Session . NHibernate , ORM: , , , .

+3

" , NHibernate :" , : . . Ayende, . , , , , ,

[Test]
public void Can_Update_Account() {
        Account account = PersistenceContext.Get<Account>(TEST_ACCOUNT_ID);

        string accountNumber = RandomString(10);
        account.AccountNumber = accountNumber;

        Account account1 = PersistenceContext.GetAll<Account>().Where(x => x.AccountNumber == accountNumber).SingleOrDefault();
        Account account2 = PersistenceContext.Get<Account>(account.Id);
        Assert.AreEqual(account.Id, account1.Id);
        Assert.AreEqual(accountNumber, account2.AccountNumber);
    }

[GetAll < > () - Linq < > .] , .

+1

, . "", , , .

0

session.Flush SaveOrUpdate, .

: SaveOrUpdate() .

, NH:

  • ,
  • The call to session.Update (entity) tells only the NHibernate session that it should start tracking the object, it does not go and does not write changes to db

So, in your case, since you already loaded the object from the session, calling session.Update () does nothing, since it is already being tracked. You could fill the power with updating the database simply by doing the following:

var session = NHibernateSessionManager.CurrentSession;
var foo = session.Linq<Foo>.ToList()[0];
foo.SomeProperty = "test";

session.Flush();

var reloadedFoos = session.Linq<Foo>
                         .Where(x => x.SomeProperty == "test");
Assert.That(reloadedFoos.Count > 0);
0
source

You need to close the session and create it before approval.

using(var session = NHibernateSessionManager.CurrentSession)
{
  using(var tx = session.BeginTransaction())
  {
    var foo = session.Linq<Foo>.ToList()[0];
    foo.SomeProperty = "test";
    session.SaveOrUpdate(foo);  
    tx.Commit();
  }
}

//create a new session here, the code depend if you use RhinoCommons (like me), no Rhino

using(var session = NHibernateSessionManager.CurrentSession)
{
  using(var tx = session.BeginTransaction())
  {
    var reloadedFoos = session.Linq<Foo>
            .Where(x => x.SomeProperty == "test");
    Assert.That(reloadedFoos.Count > 0);
    tx.Commit();
  }
}
0
source

Source: https://habr.com/ru/post/1718138/


All Articles