Here’s a subtlety of NHibernate mapping that I ran into this morning.

When you map a one-to-many relationship as a list, you can have null values in that list when loaded.

Imagine you have a typical ordering system, with Orders and OrderLines.

For demonstration purposes, please also imagine that you’ve mapped the OrderLines as List, because you want the lines to remain in specified order.

So, for Order #1023, the OrderLines table might look like this:

OrderId OrderLineId LineNumber PartId PartDescription Quantity
1023 2001 1 123 Left-handed Widget 4
1023 2002 2 124 Right-handed Widget 4
1023 2003 4 125 Universal Widget 4

Note that there’s no Line #3 in this table.

When NHibernate loads the Order Lines into memory, the List will have a Count of 4, with Lines at indexes 1, 2 and 4. At position 3 in the list will be a null.

In other words, NHibernate fully honours the LineNumber property, even if that results in null values in the list.

Oren Eini has more information.

Comments

blog comments powered by Disqus