gpt4 book ai didi

nhibernate 多对多映射 - 映射表中的附加列?

转载 作者:行者123 更新时间:2023-12-04 02:13:55 25 4
gpt4 key购买 nike

我有以下映射定义:

  <class name="Role" table="Role" optimistic-lock="version" >

<id name="Id" type="Int32" unsaved-value="0" >
<generator class="native" />
</id>

<property name="RoleName" type="String(40)" not-null="true" />

<bag name="UsersInRole" generic="true" lazy="true" cascade="all" table="UserRoles" >
<key column="RoleId" />
<many-to-many column="UserId" class="SystemUser, Domain"/>
</bag>



<id name="Id" type="Int32" unsaved-value="0" >
<generator class="native" />
</id>
<property name="UserName" type="String(40)" not-null="true" unique="true" />

此映射生成映射表 UserRoles,其中有两列 - RoleId 和 UserId。

但是,我想为该关系添加额外的属性 - 即一些定义关系状态的枚举值以及有效的开始和结束日期。

是否可以在 nhibernate 中进行,或者我是否需要在此处添加其他类并将关系 m-to-m 更改为 2 个关系 [user] 1-to-m [user_role] m-to-1 [role] ?

最佳答案

您需要添加一个额外的类,例如UserRole,在代码中保留附加属性。

当涉及到映射时,这可以像您提到的那样映射为一个类。但我也认为它可以映射为角色映射中的复合元素:

<set name="UsersInRole" lazy="true" table="UserRoles" >
<key column="RoleId" />
<composite-element class="UserRole">
<many-to-one name="User" column="UserId" not-null="true"/>
<propery name="RelationState" not-null="true"/>
<propery name="StartDate" not-null="true"/>
<propery name="EndDate" not-null="true"/>
</composite-element>
</set>

所有属性都必须不为空,因为它们已成为 UserRoles 表主键的一部分。有关更多信息,请参阅:
  • Nhibernate Composite Element Mapping
  • 7.2. Collections of dependent objects
  • 关于nhibernate 多对多映射 - 映射表中的附加列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/889513/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com