I have a one-to-many relationship where the child items for a parent are stored in an IDictonary. The mapping files are:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="NHibernateTest.Objects" assembly="NHibernateTest">
<class name="Survey" table="Survey">
<id name="Id" unsaved-value="-9999">
<column name="SvyId" sql-type="Int32" not-null="true"/>
<generator class="increment" />
</id>
<property name="Name" column="SvyName" />
More properties ..
<map name="SurveyEntries" inverse="true" cascade="all" lazy="true">
<key column="SvyId"/>
<index column="SveId" type="Int32" />
<one-to-many class="SurveyEntry" />
</map>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="NHibernateTest.Objects" assembly="NHibernateTest">
<class name="SurveyEntry" table="SurveyEntry">
<id name="Id" unsaved-value="-9999">
<column name="SveId" sql-type="Int32" not-null="true"/>
<generator class="increment" />
</id>
<property name="PropertyName" column="SvePropertyName" />
More properies ...
<many-to-one name="Survey" class="Survey" column="SvyId" />
</class>
</hibernate-mapping>
This loads my Survey object with an IDictionary of Survey Entries using:
private IDictionary surveyEntries = new Hashtable();
surveyEntries is private and I am writing class methods to access this dictionary.
I thought would be able to cast the IDictionary as a Hashtable e.g. the following code works:
IDictionary d = new Hashtable();
d.Add(1, 1);
d.Add(2, 2);
Hashtable h = d as Hashtable;
Console.WriteLine(h.ContainsKey(1).ToString());
When I execute the following I get an error:
Hashtable h = surveyEntries as Hashtable;
Any operation performed on h
The variable h is undefined after the cast and I get the exception:
An unhandled exception of type 'System.NullReferenceException' occurred in NHibernateTest.exe
Additional information: Object reference not set to an instance of an object.
I am using v1.0.2 running against an MSDE database.
Need help with Hibernate? Read this first: