views:

1407

answers:

4

Here is the scenario:

I have a winforms application using NHibernate. When launched, I populate a DataGridView with the results of a NHibernate query. This part works fine. If I update a record in that list and flush the session, the update takes in the database. Upon closing the form after the update, I call a method to retrieve a list of objects to populate the DataGridView again to pick up the change and also get any other changes that may have occurred by somebody else. The problem is that the record that got updated, NHibernate doesn't reflect the change in the list it gives me. When I insert or delete a record, everything works fine. It is just when I update, that I get this behavior. I narrowed it down to NHibernate with their caching mechanism. I cannot figure out a way to make NHibernate retrieve from the database instead of using the cache after an update occurs. I posted on the NHibernate forums, but the suggestions they gave me didn't work. I stated this and nobody replied back. I am not going to state what I have tried in case I didn't do it right. If you answer with something that I tried exactly, I will state it in the comments of your answer.

This is the code that I use to retrieve the list:

public IList<WorkOrder> FindBy(string fromDate, string toDate)
{
    IQuery query = _currentSession.CreateQuery("from WorkOrder wo where wo.Date >= ? and wo.Date <= ?");
    query.SetParameter(0, fromDate);
    query.SetParameter(1, toDate);
    return query.List<WorkOrder>();
}

The session is passed to the class when it is constructed. I can post my mapping file also, but I am not sure if there is anything wrong with it, since everything else works. Anybody seen this before? This is the first project that I have used NHibernate, thanks for the help.

A: 

what about refresh? - see 9.2. Loading an object of the docs:

"sess.Save(cat); sess.Flush(); //force the SQL INSERT sess.Refresh(cat); //re-read the state (after the trigger executes) "

Richard
+1  A: 

@Richard, your suggestion didn't pan out to well. I am still receiving the cached data.

Dale Ragan
This should be a comment on Richard's answer, not an answer itself.
brainimus
A: 

The session caches are per-session. So if you flush some changes with one session, they won't be seen by another session. You can clear the session cache explicitly with session.Clear(), or--even better-- you can use the same session throughout.

Isaac Cambron
+1  A: 

After your update, Evict the object from the first level cache.

Session.Update(obj);
Session.Evict(obj);

You may want to commit and/or flush first.

Watson