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 queries 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;
}
}