我刚开始学习Nhibernate。我有映射问题(确切 - 关系)。 我有两张桌子:
表1(DIC_TOWN)
TOWN_ID
1
2
3
表2(DIC_TOWN_STRINGS)
TOWN_ID LANG_ID TOWN_NAME
1 1 CityName
1 2 CityName
2 1 CityName
2 2 CityName
我的课程:
public class TownS
{
public virtual int TownId { get; set; }
public virtual Languages LangId { get; set; }
public virtual string TownName { get; set; }
}
修改
我的映射文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="EArchive.Data" namespace="EArchive.Data.Class">
<class name="TownS" table="DIC_TOWN_STRINGS" lazy="false">
<composite-id>
<key-property name="TownId" column="TOWN_ID" />
<key-property name="LangId" column="LANG_ID" />
</composite-id>
<property name="TownName" column="TOWN_NAME" />
<many-to-one name="LangId" column="LANG_ID"/>
</class>
</hibernate-mapping>
帮我写地图文件。如果可能的话,还有解释。感谢。
答案 0 :(得分:2)
表Dic_Town_Strings应该有一个由town_id和lang_id组成的复合键 LangId属性应该通过与您的语言类映射的多对一关系映射(或将TownS LangId属性声明更改为int而不是语言)
答案 1 :(得分:1)
为什么不
public class Town
{
public virtual int TownId { get; private set; }
public virtual Dict<Language, string> AllNames { get; private set; }
public virtual string TownName
{
get { return AllNames[CurrentLanguage]; }
}
}
映射:
<class name="Town">
<id ...>
<map name="AllNames">
<key column="TOWN_ID"/>
<index column="LANG_ID"/>
<element column="TOWN_NAME" />
</map>
周围没有复合键,一切都很直接......