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:

OrderIdOrderLineIdLineNumberPartIdPartDescriptionQuantity
102320011123Left-handed Widget4
102320022124Right-handed Widget4
102320034125Universal Widget4

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
Next Post
Using new Language and Library Features  23 Aug 2009
Prior Post
Locality Counts  12 Aug 2009
Related Posts
Superpowers for your AI  29 Mar 2026
Autotitling Windows Terminal Tabs  17 Mar 2026
Don't assume shared understanding  25 Jan 2026
Better Table Tests in Go  21 Oct 2025
Error assertions  26 Apr 2025
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
Archives
August 2009
2009