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: Problem with map of components using generic dictionary
PostPosted: Wed Jun 13, 2007 12:01 pm 
Newbie

Joined: Fri Jan 12, 2007 1:45 pm
Posts: 4
I'm using NHIBERNATE 1.2.0.GA version.

Here are the entities:

public class Union
{
private Dictionary<DateTime, Holiday2> _holidays2;
public virtual Dictionary<DateTime, Holiday2> Holidays2 { get { return _holidays2; } set { _holidays2 = value; } }

// ... other stuff ...
}

public class Holiday2
{
private string _holidayName;

public virtual string HolidayName
{
get { return _holidayName; }
set { _holidayName = value; }
}
}

Inside Union.hbm.xml, here is the map definition:

<map name="Holidays2" table="Holiday" generic="true">
<cache usage="read-write" />
<key column="UnionId" foreign-key="ApplicationId" />
<index column="HolidayDate" type="DateTime" />
<composite-element class="Holiday2">
<property name="HolidayName" column="HolidayName" type="string" />
</composite-element>

</map>

THIS FAILS:
Here is the error I get:

Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericMap`2[System.DateTime,Braveline.WRS.Core.Holiday2]' to type 'System.Collections.Generic.Dictionary`2[System.DateTime,Braveline.WRS.Core.Holiday2]'.
I've been banging my head on the keys for this one, even trying to debug the NHibernate code...
any ideas, suggestions???

HOWEVER: THIS WORKS:
Please note that it works for a map of elements (ie. a map of strings).
That is, it works for this scenario:

public class Union
{
private Dictionary<DateTime, string> _holidayNames;
public virtual Dictionary<DateTime, string> HolidayNames { get { return _holidayNames; } set { _holidayNames = value; } }
}

Union.hbm.xml:
<map name="HolidayNames" table="Holiday" generic="true">
<cache usage="read-write" />
<key column="UnionId" foreign-key="ApplicationId" />
<index column="HolidayDate" type="DateTime" />
<element column="HolidayName" type="string" />
</map>

So in conclusion, a map of elements (simple data type) using a generic dictionary works.
However, a map of components (ie. composite-element, ie. a class) using a generic dictionary fails.

Thanks for your help!!

Michael


Top
 Profile  
 
 Post subject: Re: Problem with map of components using generic dictionary
PostPosted: Thu Jun 14, 2007 8:02 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
mpnet wrote:
I'm using NHIBERNATE 1.2.0.GA version.

Here are the entities:

public class Union
{
private Dictionary<DateTime, Holiday2> _holidays2;
public virtual Dictionary<DateTime, Holiday2> Holidays2 { get { return _holidays2; } set { _holidays2 = value; } }

// ... other stuff ...
}

public class Holiday2
{
private string _holidayName;

public virtual string HolidayName
{
get { return _holidayName; }
set { _holidayName = value; }
}
}

Inside Union.hbm.xml, here is the map definition:

<map name="Holidays2" table="Holiday" generic="true">
<cache usage="read-write" />
<key column="UnionId" foreign-key="ApplicationId" />
<index column="HolidayDate" type="DateTime" />
<composite-element class="Holiday2">
<property name="HolidayName" column="HolidayName" type="string" />
</composite-element>

</map>

THIS FAILS:
Here is the error I get:

Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericMap`2[System.DateTime,Braveline.WRS.Core.Holiday2]' to type 'System.Collections.Generic.Dictionary`2[System.DateTime,Braveline.WRS.Core.Holiday2]'.
I've been banging my head on the keys for this one, even trying to debug the NHibernate code...
any ideas, suggestions???


You need to change your Dictionary<DateTime, Holiday2> to be an IDictionary<DateTime, Holiday2>. NHibernate changes your collection at run time to be it's own persistence aware collection. Provided your initial collection (Dictionary) and it's own collection PersistentGenericMap utilize the same interfaces it can change out your collection for its own.

Change your class to use an interface for it's property and field instead, and all of your issues will go away.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 10:55 am 
Newbie

Joined: Fri Jan 12, 2007 1:45 pm
Posts: 4
Thanks for your reply!

I realized this was the problem late afternoon yesterday - the light bulb over my head went off - and I realize that the collections must be of the interface type since NHibernate will inject its own implementation.

It all works now. Thanks.

Michael


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.