nHibernate 在刷新会话时删除并重新插入多对多值

发布于 2024-10-01 22:12:07 字数 1109 浏览 0 评论 0原文

考虑下表映射,这只不过是经典的用户安全角色映射示例。

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.User, Domain" table="[User]">
<id name="UserID" column="UserId" type="Int32">
  <generator class="hilo">
    <param name="table">NhibernateUniqueKey</param>
    <param name="column">NextHi</param>
    <param name="where">TableName='[User]'</param>
    <param name="max_lo">20</param>
  </generator>
</id>
<property name="UserName" column="UserName" type="String" length="20"/>
<property name="Password" column="Password" type="String" length="20"/>
.
.
<bag name="SecurityRoles" table="UserSecurityRole" lazy="false">
  <key column="UserId"></key>
  <many-to-many class="Model.SecurityRole, Domain"          column="SecurityRoleId"></many-to-many>
</bag>

问题是,当我通过用户名[使用条件查询]检索用户,并刷新会话时,附加到检索到的用户的所有安全角色都会被删除并再次重新插入,尽管我没有对检索到的对象进行任何修改。

我在这里做错了什么还是这就是 nhibernate 设计的工作方式?

Consider the Following table mapping, this is not more than classic User security role mapping example.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.User, Domain" table="[User]">
<id name="UserID" column="UserId" type="Int32">
  <generator class="hilo">
    <param name="table">NhibernateUniqueKey</param>
    <param name="column">NextHi</param>
    <param name="where">TableName='[User]'</param>
    <param name="max_lo">20</param>
  </generator>
</id>
<property name="UserName" column="UserName" type="String" length="20"/>
<property name="Password" column="Password" type="String" length="20"/>
.
.
<bag name="SecurityRoles" table="UserSecurityRole" lazy="false">
  <key column="UserId"></key>
  <many-to-many class="Model.SecurityRole, Domain"          column="SecurityRoleId"></many-to-many>
</bag>

The problem is when i retrive the user by username [using a criteria query], and flush the session all the security roles attached to the retrieved user gets deleted and re-inserted again, although i do not make any modifications to the retrived object.

Am i doing something wrong here or is this the way nhibernate works by design ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖哒 2024-10-08 22:12:07

这就是 bag 的行为。为了避免这种情况,请使用idbagset

That's the behavior of bag. To avoid it, use idbag or set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文