Just sharing some of my inconsequential lunch conversations with you... RSS  

Wednesday, October 10, 2007

The case of the NHibernate disappearing item

A colleague of mine, Diogo Henriques, was implementing a use case using NHibernate when he call me for some pair-debugging with the following problem:

Diogo was using a set to map a one-to-many relationship, mapped as follow:


<set name="Lines" cascade="all">
<key column="ID_INVOICE">
<one-to-many class="Model.InvoiceLine, Model">
</one-to-many>
</key>
</set>


This was a pattern we were quite comfortable with - we created the invoice and added the lines through the set. But this time with a twist: after inserting the lines, and over the same session, we iterated through the set, picked up a particular line and updated it. The result was a total surprise: this item just didn't got persisted into the database! Better yet, no exception was raised!

After some debugging Diogo identified the problem: we weren't (as we shouldn't have to) setting InvoiceLine.Invoice corresponding inverse relationship, and NHibernate engine for some strange reason lost the association context when updated. It was somehow detached from the context that guaranteed the persistence.

As anybody found this behavior in HNibernate?

No comments:

Development Catharsis :: Copyright 2006 Mário Romano