Mapping a collection of Enums with NHibernate

Just came across a situation where I needed to have a collection of enum values mapped to an IList and to have it supported by NHibernate. It took me a little bit to find the proper approach and get NHibernate to play nicely. So as a reference for anyone else running into this you can find some information at this posting here

Here is a snippet of the bag at hand:

<bag name=”companyRoleList” access=”field” lazy=”false” cascade=”none” table=”CompanyRole” >
    <key column=”CompanyID” />
    <element column=”RoleID” type=”MyCompany.Domain.Lookups.CompanyRoleType, MyCompany.Domain.Lookups” />

This is from the blog post on the NHibernate forum. The trick here is to have the full namespace/assembly qualification in the type attribute that points to the Enum you are using as a collection. This is needed even if you have the namespace=, assembly= at the hibernate-mapping element. That stumped for a little while.

Hopefully this helps someone else out!

About Sean Chambers

I am a Senior software developer from Palm Coast, Florida. An advocate of Domain Driven Design, Behavior Driven Development, creator of FluentMigrator and community activist. I am married to my beautiful wife Erin and am the proud father of two wonderful children. I currently reside at ACI, a local insurance industry/mortgage software company that excels in creating solutions using Agile methodologies.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Jozef Sevcik

    How is it possible to query such bag, I mean, in your example get all employees that has specified CompanyRoleType(s).
    When I try something like:
    Express.In(“CompanyRoleList”, new CompanyRoleType[] { CompanyRoleType.Administrator });
    it gives me ‘cannot use collection InExpression’.
    Is it possible to query/filter such bag ? Is it possible via ICriteria ?