Nhibernate QueryOver does not receive recent database changes

I am trying to get a record updated from a database using QueryOver. My code initially creates an entity and stores it in the database, then the same record is updated in the database from the outside (from another program, manually or from the same program running on another machine), and when I call QueryOverfiltering by field, the request receives a record but without the latest changes.

This is my code:

//create the entity and save in database
MyEntity myEntity = CreateDummyEntity();
myEntity.Name = "new_name";

MyService.SaveEntity(myEntity);

// now the entity is updated externally changing the name property with the 
// "modified_name" value (for example manually in TOAD, SQL Server,etc..)

//get the entity with QueryOver
var result = NhibernateHelper.Session
                 .QueryOver<MyEntity>()
                 .Where(param => param.Name == "modified_name")
                 .List<T>();

The previous statement receives a collection with only one record (good), BUT with the set name with the old value instead of "modified_name".

How can I fix this behavior? Does level 1 cache bother me? The same problem occurs when

CreateCriteria<T>();

NhibernateHelper - , , session.Save(). , , -, , .

SQL NHibernate, WHERE ( Nhibernate ), !!!!

UPDATE

SaveEntity session.Save: Commit

public virtual void Commit() 
{ 
  try 
  { 
    this.session.Flush(); 
    this.transaction.Commit();
  } 
  catch 
  { 
    this.transaction.Rollback(); 
    throw; 
  } 
  finally 
  { 
    this.transaction = this.session.BeginTransaction();
  } 
}

SQL, NHibernate SaveEntity:

NHibernate: INSERT INTO MYCOMPANY.MYENTITY (NAME) VALUES (:p0);:p0 = 'new_name'. 

SQL, NHibernate QueryOver:

NHibernate: SELECT this_.NAME as NAME26_0_ 
            FROM MYCOMPANY.MYENTITY this_ 
            WHERE this_.NAME = :p0;:p0 = 'modified_name' [Type: String (0)]. 

- .

.

+5
6

, ... .

: , QueryOver<T>() , . , HibernateUtil.Initialize(entity) lazy="false" . lazy="false" , . ( ), lazy="true" HibernateUtil.Initialize(entity) , ; , , , HibernateUtil.Initialize(collection) .

. @martin ernst, ​​ , .

0

, :

  • IStatelessSession, sessionFactory.OpenStatelesSession() sessionFactory.OpenSession()
  • Session.Evict(myEntity)
  • Session.Clear() , QueryOver
  • CacheMode Ignore, Put or Refresh QueryOver ( )

, , (, IMHO, , , )

+4

session.Save(myEntity) *. , session.Flush() , . nhibernate .

. , Get Load, .. session.Get<MyEntity>(1) , MyEntity id 1 , session.QueryOver<MyEntity>().Where(x => x.id == 1) .

NHibernate .

:

  • SaveEntity, ..

    using (var transaction = Helper.Session.BeginTransaction())
    {
      Helper.Session.Save(myEntity);
      transaction.Commit();
    }
    
  • session.Flush() SaveEntity, ..

      Helper.Session.Save(myEntity);
      Helper.Session.Flush();
    

- all.

* , , - Identity id.

+1

:

 var result = NhibernateHelper.Session
             .QueryOver<MyEntity>()
             .CacheMode(CacheMode.Refresh)
             .Where(param => param.Name == "modified_name")

, :

NhibernateHelper.Session.Refresh(result);
+1

Session.Clear(), . , Session.Evict() , . - .
, Evict() . , .
, - "" - , , ... , Evict() - , .

var cachedObjects = NhibernateHelper.Session
             .QueryOver<MyEntity>()
             .Where(param => param.Name == "modified_name")
             .List<T>();

foreach (var obj in cachedObjects) 
    NhibernateHelper.Session.Evict(obj);

var freshObjects = NhibernateHelper.Session
             .QueryOver<MyEntity>()
             .Where(param => param.Name == "modified_name")
             .List<T>()
0

- , NHibernate. (cascade: all), ISession.Flush(). , . . QueryOver JoinAlias, the generated SQL query looks great, and the rows return correctly, however, the found collection that should receive these new children has already been initialized in the session (as it should be), and based on this, NH decides for some reason to completely ignore matching strings. I think NH makes the wrong assumption that if the collection is already marked as "Initialized", it does not need to be reloaded from the request. It would be great if someone more familiar with NHibernate's internal structures could hear this.

0
source

All Articles