-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Casting IDictionary as Hashtable
PostPosted: Wed Apr 05, 2006 2:44 am 
Newbie

Joined: Mon Apr 03, 2006 5:12 pm
Posts: 15
Location: Glasgow, Scotland
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:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 3:52 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
NHibernate replaces your collections with its own implementations of their respective interfaces, so if your object was saved or loaded from the database, then the collection it has is actually not a Hashtable but NHibernate.Collection.Map.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 9:19 am 
Newbie

Joined: Mon Apr 03, 2006 5:12 pm
Posts: 15
Location: Glasgow, Scotland
Thanks for that. I want to write two methods to get an object from the collection, either by the key or index. Is this the most efficient way:

Code:
public SurveyEntry GetSurveyEntryById(int id)
{
   NHibernate.Collection.Map map = this.surveyEntries as NHibernate.Collection.Map;
   return (SurveyEntry)map[id];
}


Code:
public SurveyEntry GetSurveyEntryByIndex(int index)
{
   NHibernate.Collection.Map map = this.surveyEntries as NHibernate.Collection.Map;
   int i = 0;
   foreach (object o in map.Values)
   {
      SurveyEntry entry = (SurveyEntry)o;
      if (i == index)
         return entry;
      i++;
   }
   return null;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.