NHibernate not persisting owned objects!

I have just gotten a very odd error and I can’t explain, or trace it out. My forum entity maintains a list of child forums. The list is internal, and exposed as an ienumerable, along with some methods like AddChild, FindChild and RemoveChild. Now to the code:

[HttpPost, ValidateAntiForgeryToken]
public ActionResult Add(ForumAddModel model, int? forumId = null)
    if (!ModelState.IsValid) return View(model);
    Forum entity = new Forum(model.Title);
    entity.Sequence = model.Sequence;
    entity.Description = model.Description;
    entity.Moderated = model.Moderated;
    entity.Enabled = model.Enabled;
    entity.Visible = model.Visible;
    if (forumId.HasValue)
        Forum parent = forumRepository.SelectById(forumId.Value);
        return RedirectToAction("Detail", new { id = forumId });
        return RedirectToAction("Manager");

I have stepped through this several times, testing values along the way. It works perfectly, so I cannot explain the error. If a parent forum id is given, forumRepository.Update(parent) runs with no errors, it just doesn’t actually save the new child to the db.

Incidentally… until I restart VS2010, the nhibernate session actually THINKS it HAS worked, and the new record shows on the page.

Say whut?

Heh! This always happens to me. I post an issue, then find the answer right before somebody posts a reply. Makes me feel a bit silly.

There were actually TWO issues (both now solved):

  1. I had an instance.Inverse() set where I shouldn’t have had.
  2. I forgot to wrap the Insert, Update, and Delete methods in a transaction, so nothing was ever committed!

Duh…thanks for replying though. =)

Could you publish the parent.AddForum(…) and the forumRepository.Update(…) method, please? The Hibernate entity classes would be probably helpfull too…


NHibernate only persists objects to the database when the session is flushed which either happens via session.Flush() or when a transaction is committed :wink: