Exception trying to map a SortedList

36 views
Skip to first unread message

Arcreme

unread,
Mar 3, 2012, 4:40:12 PM3/3/12
to nhusers
I am trying to map the following SortedList:

...
private SortedList<DateTime, string> _data;
public virtual SortedList<DateTime, string> Data
{
get { return _data; }
set { _data = value; }
}

...
<map name="Data" sort="natural">
<key column="EntityId" />
<index column="time" type="DateTime" />
<element column="value" type="String" />
</map>
...

However when I try to create a session factory using
Configuration.BuildSessionFactory() I get a NullReferenceException:

--NullReferenceException
at NHibernate.Mapping.Map.get_CollectionType() in d:\CSharp\NH\NH
\nhibernate\src\NHibernate\Mapping\Map.cs: line 33

If I omit the sort attribute (which I belive I shouldn't according to
the manual) in the map element i get a
NHibernate.PropertyAccessException:

System.InvalidCastException : Unable to cast object of type
'NHibernate.Collection.Generic.PersistentGenericMap`2[System.DateTime,System.String]'
to type
'System.Collections.Generic.SortedList`2[System.DateTime,System.String]'.

I also to have the sort attribute to point to a implementation of the
IComparer interface, but I still get the NullReferenceException.

Any ideas on how to succesfully map a SortedList?

Thank you.

Ricardo Peres

unread,
Mar 3, 2012, 5:07:42 PM3/3/12
to nhu...@googlegroups.com
Map the Data property through an interface, such as:

public virtual IDictionary<DateTime, string> Data 

    get;
    set;
 

Arcreme

unread,
Mar 5, 2012, 5:57:49 AM3/5/12
to nhusers
Hi Ricardo

Thanks for your reply. I'm afraid i havn't made any progress yet. I
tried to change the signature of the Data property with two different
implementations. In the first I use a private field, so I'm able to
initialse the field in the constructor. In the second I use an auto-
implemented property (which I think is the one that u suggest).
However both of these of these implementations gives me the
NullReferenceException when calling
Configuration.BuildSessionFactory().

Does anyone have a code example of how to map a SortedList<,>?

Thank you

TestEntity implementation 1:

namespace Domain
{
public class TestEntity
{
private SortedList<DateTime, string> _data;

public TestEntity()
{
_data = new SortedList<DateTime, string>();
}

public virtual Guid Id { get; set; }

public virtual IDictionary<DateTime, string> Data
{
get { return _data; }
set
{
_data = value is SortedList<DateTime, string>
? (SortedList<DateTime, string>)value
: new SortedList<DateTime, string>(value);
}
}
}
}

TestEntity implementation 2:
namespace LiveBall.Domain.Participant
{
public class TestEntity
{
public TestEntity()
{ }

public virtual Guid Id { get; set; }

public virtual IDictionary<DateTime, string> Data
{
get;
set;
}
}
}

TestEntity.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Domain" namespace="Domain">
<class name="TestEntity">
<id name="Id">
<generator class="guid" />
</id>
<map name="Data" sort="natural">
<key column="EntityId" />
<index column="time" type="DateTime" />
<element column="value" type="String" />
</map>
</class>
</hibernate-mapping>
> > 'NHibernate.Collection.Generic.PersistentGenericMap`2[System.DateTime,Syste­m.String]'

Michael Charalambous

unread,
Mar 7, 2012, 4:33:16 AM3/7/12
to nhu...@googlegroups.com
Hi

In the map element you haven't specified the table:

<map name="Data" sort="natural"  table="TABLE NAME GOES HERE">
      <key column="EntityId" />
      <index column="time" type="DateTime" />
      <element column="value" type="String" />
    </map>




Michael Charalambous

unread,
Mar 7, 2012, 4:42:59 AM3/7/12
to nhu...@googlegroups.com
Also please check out Ayende's post on the map element. His first example of map's use is very close to what you are trying to achieve.

http://ayende.com/blog/4045/nhibernate-mapping-map

Regards
Michael
Reply all
Reply to author
Forward
0 new messages