讓我們保持了DB模式,因爲它是和第一次調整C#域類:
public class Person
{
public virtual string Name {get;set;}
public virtual int Age {get;set;}
public virtual IList<Role> RolesForThisPerson {get;set;}
}
public class Role
{
public virtual string RoleName { get; set; }
}
這兩個實體現在基本映射到定義的表:
<class name="Person" table="Person" lazy="true">
<id name="ID" column="personID">
<generator class="native" />
</id>
<property name="Name" not-null="true" />
<property name="Age" not-null="true" />
<!-- placeholder for roles -->
</class>
<class name="Role" table="Role" lazy="true">
<id name="ID" column="roleID">
<generator class="native" />
</id>
<property name="RoleName" not-null="true" />
</class>
現在我們可以使用<idbag>
映射和擴展Person
類映射這種方式:
<idbag name="RolesForThisPerson" batch-size="25" table="PersonRoles"
inverse="true" lazy="true" cascade="none" >
<collection-id column="personRolesID" type="System.Int32" >
<generator class="native" />
</collection-id>
<key column="personID" />
<many-to-many class="Role" column="roleID" />
</idbag>
<idbag>
可以從這個事實中獲益,甚至對錶也有它自己的標識符。 Cascade設置爲none,期望角色在系統中,用戶只能分配給他們(從中刪除)。屬性batch-size
將影響在提取懶惰角色集合時將執行多少個SELECT語句。
,謝謝你。現在有點清楚了。你有幾個問題。我閱讀了那些有關建模類(DDD)的博客之一,你不應該在你的實體中擁有一個集合,而不是封裝該集合的對象(我個人認爲這是對抽象的浪費)。因此,我的示例顯示角色和集合內部。對此有何看法? Inverse = true ...我現在正在對此進行一些研究。 –