开发者

FindAll after SaveOrUpdate not returning updates

I have a method that queries the database (in this case a view) for parent records, gets and updates a child of each parent record, saves the child record, and then queri开发者_如何学Pythones for the parent records. There is a one-to-one relationship between the child and parent but the relationship is not defined in the mapping files. I can see that the updates are being saved to the database by issuing a select to the database directly but when I do the second query in code the updates are not included in the results. Why is the second query not returning the updates?

Rough outline of code:

public void UpdateRecords(long aParentId)
{
   IList<Parent> parents = parentRepository.GetParentById(aParentId);
   foreach (Parent parent in parents)
   {
      Child childToUpdate = childRepository.GetChildById(parent.GetChildId());

      ... Update Child ...

      childRepository.SaveChild(childToUpdate);
   }
   IList<Parent> parents = parentRepository.GetParentById(aParentId);
}

class ParentRepository : NHRepository
{
   public IList<Parent> GetParentById(long anId)
   {
      DetachedCriteria criteria = 
         DetachedCriteria.For<Parent>()
         .Add(Restrictions.Eq("Id", anId));

      return FindAll(criteria).ToList();
   }
}

class ChildRepository : NHRepository
{
   public Child SaveChild(Child aChild)
   {
      Child savedChild = null;
      using (UnitOfWork.Start())
      {
         savedChild = base.SaveOrUpdate(aChild);
         UnitOfWork.Current.Flush(); // commit
      }
      return savedChild;
   }
}


What does your FindAll() method look like? It's possible that it's using same UnitOfWork/Session for both GetParentById() calls and that child repository is using another UnitOfWork/Session.

If that's the case, then the second GetParentById() call is returning object from session (first level) cache without hitting the database.

Edit: You can browse the source code of Rhino.Commons on:

https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk/commons/Rhino.Commons.NHibernate/

Session is encapsulated in Rhino.Commons.UnitOfWork class. I'm not familiar with Ayende's implementation of UnitOfWork, so I won't be able to give you the best solution for this, but here's something to get you started....

Try to call

Session.Clear();

at the top of your GetParentById() method.

I repeat, you'll probably need to find a better way to do this (probably by using UnitOfWork), but this should clear all objects from your session cache and hence load new ones from database on next request.


Try surrounding your SaveUpdate() in a transaction. Then commit the transaction when done, this will ensure your changes are written to the database right when you commit:

Something like this is fairly common to see:

using (var trans = session.BeginTransaction()) {
  session.SaveUpdate(e);
  trans.Commit()
} catch (Exception) {
  trans.Rollback()
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜