List vs Set vs Bag in NHibernate

NHibernate semantics:

  1. List: Ordered collection of entities, duplicate allowed. Use a .NET IList in code. The index column will need to be mapped in NHibernate.

  2. Set: Unordered collection of unique entities, duplicates not allowed. Use Iesi.Collection.ISet in code (NH prior to v4) or System.Collections.Generic.ISet (NH v4+). It is important to override GetHashCode and Equals to indicate the business definition of duplicate. Can be sorted by defining an orderby or by defining a comparer resulting in a SortedSet result.

  3. Bag: Unordered list of entities, duplicates allowed. Use a .NET ICollection<T> in code. The index column of the list is not mapped and not honored by NHibernate.

Leave a Comment