How can I get the latest data from a database using NHibernate after an update?

Here is the scenario:

I have a winforms application using NHibernate. On startup, I populate the DataGridView with the results of the NHibernate query. This part is working fine. If I update an entry in this list and clear the session, the update takes in the database. After closing the form after updating, I call the method to retrieve the list of objects, to populate the DataGridView again, to get the changes, as well as to get any other changes that might have happened to someone else. The problem is that the updated record, NHibernate does not reflect the changes in the list that it gives me. When I insert or delete a record, everything works fine. Just when I update, I get this behavior. I narrowed it down to NHibernate with my caching mechanism. I cannot figure out how to extract NHibernate from the database, and not use the cache after the update. I posted on the NHibernate forums, but the suggestions they gave me didn't work. I said this and no one answered. I am not going to state that I tried if I do not do it right. If you respond with what I tried for sure, I will indicate it in the comments of your answer.

This is the code 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>(); } 

A session is passed to the class when it is built. I can also publish the mapping file, but I'm not sure if something is wrong with it, as everything else works. Has anyone seen this before? This is the first project I used NHibernate, thanks for the help.

+4
source share
2 answers

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

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

You can first commit and / or flush.

+3
source

how about an update? - see 9.2. Loading an object from documents:

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

0
source

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


All Articles