I have 3 entities:
class User {id,name...}
class UserUrl {id,user_id,url,url_type_id}
class UrlType {id,name}
My mapping:
<class name="User" table="Users" lazy="false">
<id name="id" type="Int32" column="id">
<generator class="identity" />
</id>
<property name="name" column="name" type="String"/>
<map name="Urls" table="UserUrl">
<key column="user_id"></key>
<index-many-to-many class="UrlType" column="url_type_id"/>
<one-to-many class="UserUrl"/>
</map>
</class>
<class name="UserUrl" table="UserUrl">
<id name="id" type="Int32" column="id">
<generator class="identity"/>
</id>
<property name="user_id" column="user_id" type="Int32"/>
<many-to-one name="UrlType" column="url_type_id" class="UrlType"/>
<property name="Url" column="url" type="String" not-null="true"/>
</class>
>
So User.Urls is IDictionary<UrlType,UserUrl>
. But I want to get Dictionary<string,UserUrl>
, where string key is UrlType.name.
Does anybody know how to do this?
views:
140answers:
2
A:
NHibernate will give you an interface for collections, in this case IDictionary as its implementation will involve proxies and caching and you won't want to know the details. So you won't get a Dictionary.
My question would be why do you want a Dictionary, you access all the data via the IDictionary interface what extra functionality would a concrete class give?
Mark
2009-08-05 13:00:57
Example:class User{ .... IDictionary<string,UserUrl> _urls; IDictionary<string,UserUrl> Urls { get{return _urls;} set{_urls=value;} }}...User curUser = FindById(2);string facebookUrl = curUser.Urls["facebook"];So I think it is very usefull to get IDictionary than IList, because to get facebookUrl there is no need to inerate each element in Ilist.
2009-08-06 10:39:55
A:
I find out desition.
Mapping:
<class name="User" table="Users" lazy="false">
<id name="id" type="Int32" column="id">
<generator class="identity" />
</id>
<property name="name" column="name" type="String"/>
<map name="Urls" lazy="true" cascade="all-delete-orphan" inverse="true">
<key column="user_id"></key>
<index column="im_type_id" type="Int32"/>
<one-to-many class="UserUrl"/>
</map>
</class>
<class name="UserUrl" table="UserUrl">
<id name="id" type="Int32" column="id">
<generator class="identity"/>
</id>
<property name="user_id" column="user_id" type="Int32"/>
<property name="UrlType" column="url_type_id" type="Int32" not-null="true" />
<property name="Url" column="url" type="String" not-null="true"/>
</class>
Code:
public sealed class UrlType
{
private const string _Facebook = "Facebook";
private const string _Myspace = "Myspace";
private const string _Youtube = "Youtube";
public static readonly int Facebook;
public static readonly int Myspace;
public static readonly int Youtube;
static UrlType()
{
Facebook = FindByName(_Facebook).Id;
Myspace = FindByName(_Myspace).Id;
Youtube = FindByName(_Youtube).Id;
}
}
Use:
User curUser = FindById(2);
string facebookUrl = curUser.Urls[UrlType.Facebook].Url;
But It is spend more time to get ids of Urls types from DB
Kate
2009-08-13 09:09:14