Please help, i can't figure out how to put collection items to an IDictionary indexed by the third class, There is an example in documentation, but i can't figure out the complete setup:
http://www.hibernate.org/hib_docs/nhibernate/html_single/#collections-ternary
I have three tables
Language(Id, ... )
GlobalString(Id, Name )
GlobalStringLocalized(GlobalStringId, LanguageId, Value )
mapping
Code:
<class name="GlobalString" table="GlobalString" >
<id name="ID" type="System.Int32" column="ID">
<generator class="identity"/>
</id>
<property name="Name" column="Name" type="System.String" not-null="true" length="200"/>
<bag name="GlobalStringlocalizeds" table="GlobalStringlocalized" inverse="true" lazy="true" cascade="delete">
<key column="GlobalStringID" />
<one-to-many class="GlobalStringlocalized"/>
</bag>
</class>
<class name="GlobalStringlocalized" table="GlobalStringlocalized" >
<composite-id name="ID" class="GlobalApp.Core.Domain.GlobalStringlocalized+DomainObjectID">
<key-property type="System.Int32" name="GlobalStringID" column="GlobalStringID" />
<key-property type="System.Int32" name="LanguageID" column="LanguageID" />
</composite-id>
<property name="Value" column="Value" type="System.String" not-null="true" />
<many-to-one name="GlobalStringIDGlobalString" column="GlobalStringID" class="GlobalString" update="0" insert="0" />
<many-to-one name="LanguageIDLanguage" column="LanguageID" class="Language" update="0" insert="0" />
</class>
<class name="Language" table="Language" >
<id name="ID" type="System.Int32" column="ID">
<generator class="identity"/>
</id>
<property name="LanguageTag" column="LanguageTag" type="System.String" not-null="true" length="10"/>
<property name="NativeName" column="NativeName" type="System.String" not-null="true" length="30"/>
<bag name="GlobalStringlocalizeds" table="GlobalStringlocalized" inverse="true" lazy="true" cascade="delete">
<key column="LanguageID" />
<one-to-many class="GlobalStringlocalized"/>
</bag>
<bag name="StoreLocalizeds" table="StoreLocalized" inverse="true" lazy="true" cascade="delete">
<key column="LanguageID" />
<one-to-many class="StoreLocalized"/>
</bag>
</class>
DomainCode:
public class GlobalString : DomainObject<System.Int32>
{
//...
public virtual IList<GlobalStringlocalized> GlobalStringlocalizeds{
get { return _GlobalStringlocalizeds; }
set { _GlobalStringlocalizeds = value; }
}
//...
}
With this setup i have to use queries to get localized fields of a given language:
DaoCode:
public class AbstractDaoLocalized<GlobalizedType, LocalizedType>
{
public IList<LocalizedType> GetLocalizedBy(GlobalizedType entity, Language language)
{
ISession session = NHibernateSessionManager.Instance.GetSession();
string name = typeof(GlobalizedType).Name;
return session.CreateCriteria(typeof(LocalizedType))
.Add(Expression.Eq(name + "ID" + name, entity))
.Add(Expression.Eq("LanguageIDLanguage", language))
.List<LocalizedType>();
}
}
How do i change the mapping to use IDictionary<Language, GlobalStringLocalized> instead of IList<GlobalStringLocalized>?
so i could avoid using multiple Daos?
This is how i want it to be:
Code:
... GetGlobalStrings(Language language)
...
foreach(GlobalString gs in session.CreateCriteria(typeof(GlobalString)).List<GlobalString>())
{
string byLanguage = gs[language].Value;
// or
// string byLanguage = gs[language.Id].Value;
..
}
public class GlobalString : DomainObject<System.Int32>
{
//...
public virtual IDictionary<Language, GlobalStringLocalized> GlobalStringlocalizeds
{
get { return _GlobalStringlocalizeds; }
set { _GlobalStringlocalizeds = value; }
};
//...
}
// or
public class GlobalString : DomainObject<System.Int32>
{
//...
public virtual IDictionary<Int32/*Language.Id*/, GlobalStringLocalized> GlobalStringlocalizeds
{
get { return _GlobalStringlocalizeds; }
set { _GlobalStringlocalizeds = value; }
};
//...
}