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.


blog comments powered by Disqus
Next Post
Using new Language and Library Features  23 Aug 2009
Prior Post
Locality Counts  12 Aug 2009
Related Posts
Browsers and WSL  31 Mar 2024
Factory methods and functions  05 Mar 2023
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
August 2009