R – Ordered many-to-many relationship in NHibernate

many-to-manynhibernatenhibernate-mapping

Let's say I have two classes: Item and ItemCollection, where ItemCollection contains an ordered list of Item objects with an index, i.e. the list is ordered in a way specified by the user.

Let's also say that they have a many-to-many relationship, an ItemCollection can contain many items and an Item can belong to several ItemCollections.

That would, in my head, require three tables in the database. One for Item, one for ItemCollection and one for the ordered mapping. The mapping table would contain three columns:

int ItemID
int ItemCollectionID
int ListIndex

QUESTION: How would you design the ItemCollection class? Should the list of Item objects be a list, dictionary or other? What would the NHibernate mapping look like to get the ListIndex into the picture?

Best Answer

If you're using Fluent NHibernate, you'll find an answer in the HasMany mapping in the code for OrderMapping in this article.

For traditional hibernate mapping (XML file), see chapter 21.3 of the NHibernate documentation here.