nHibernate 在刷新会话时删除并重新插入多对多值
考虑下表映射,这只不过是经典的用户安全角色映射示例。
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是 bag 的行为。为了避免这种情况,请使用
idbag
或set
。That's the behavior of bag. To avoid it, use
idbag
orset
.