This is my first time with NHibernate, but I've been playing with this for a number of hours and can't seem to find anything in either the docs or on the board archives. I'm having a problem getting a one-to-many association working with a Map/IDictionary. Here's my domain:
- A patient has a collection of phone numbers. Each number has a type (home, work, etc.) I would like to have Patient expose a "PhoneNumbers" collection, indexable by type.
I have defined a Patient class and a PhoneNumber class. For patient, I have the following in the hb.xml to define the 1..* relationship:
<map name="PhoneNumbers" table="PhoneNumber" cascade="all" access="nosetter.camelcase-underscore">
<key column="Type"/>
<index column="PhoneNumberKey" type="Int32" />
<one-to-many class="PhoneNumber, MyAssembly" />
</map>
And in the PhoneNumber hb.xml I have:
<many-to-one name="PatientKey" column="PatientKey" class="Patient, MyAssembly" cascade="none" />
Originally I had intended to index the IDictionary by an enum of PhoneNumberType (work, home, etc.) When I encountered this exception:
>>
NHibernate.ADOException : Could not save object
----> NHibernate.ADOException : Could not save object
----> System.ArgumentException : There is a problem with your mappings. You are probably trying to map a System.ValueType to a <class> which NHibernate does not allow or you are incorrectly using the IDictionary that is mapped to a <set>.
A ValueType can not be used with IdentityKey. The thread at google has a good description about what happens with boxing and unboxing ValueTypes and why they can not be used as an IdentityKey:
http://groups.google.com/groups?hl=en&l ... 26tab%3Dwg
Parameter name: key
<<
I figured the <key/> element was the problem. I updated the schema for the phone number type to be an nvarchar and retried with no success. I haven't found many examples of using the map element anywhere, so I'm likely overlooking something.
Tomorrow I'll try using IList instead as there are more examples of this, but I'd like the indexer lookup if possible in my model. Any help is appreciated.
Thanks!