NHIbernate Update

I know this has been asked, but I cannot solve this problem.

Suppose you have an nhibernate object that has a collection.

The problem is that if any object in the collection is updated in the database by another user (I manually change the database testing goals), I cannot find a way to make nhibernate update the value of the collection.

I am trying to update, evict, Loading againg ... Only closing the session and creating a new one. But I find this solution problematic and how hard is it not to say nhibernate "dude, update items in the collection"?

But somehow, I can’t get it to work.

thanks a lot

+6
source share
3 answers

Actually this should not work, therefore it is not.

NHibernate uses session level cache by default to optimize reading and track changes.

It seems that you are not using the correct unit of work with your access to data, since your sessions should be limited and used only when you need it.

If this is part of a web application, it is recommended that you use a session per request.

If you really wanted this to work, you could probably evict the entity, and these are the children from the session, forcing re-fetching, but this is nasty, and I would not recommend it.

+1
source

Having got into trouble, trying to evict, and even considered opening and closing sessions. It looks like there is enough free space in your code to consider creating a filtered get, i.e. Get all elements of the collection type, where the parent identifier is the parent. You may need to expand your repository or Tao depending on how you work with NH, but as others have said, it's hard to be more specific without an example. The main thing is that you get the child objects in the request for this type, filtered by the parent identifier.

This will be a new get, so you will get new or changed objects created by some other process, and then you can set this collection as a collection in the parent object. NH must overcome this when trying to save, as it can make assumptions about your facilities and keep or update as needed. This can be useful for you if there is a possibility of a delay in the time between receiving the parent and saving it again.

The problem with this approach is that a merge type operation is required. You might want to add any new objects that were created in the process so that you do not lose your changes when updating the collection. Hope this helps, give us more information if you are still stuck.

0
source

You must implement the IDispose interface in your SessionFactory class.

Similar:

public class YouSessionFactory : INHibernateSessionFactory, IDisposable { private ISessionFactory _sessionFactory; //codes for initial _sessionFactory, for configuration,mapping or something else. //balalalalalala //.... public ISessionFactory BuildSessionFactory() { return _sessionFactory; } public void Dispose() { if (!_sessionFactory.IsClosed || _sessionFactory != null) { _sessionFactory.Close(); _sessionFactory.Dispose(); } } } 
0
source

All Articles