我有一个Nhibernate映射文件,用于简单的用户/角色映射。
以下是映射文件:
Users.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Sample.Persistence" namespace="Sample.Persistence.Model">
<class name="User" table="Users">
<id name="UserKey">
<generator class="identity"/>
</id>
<property name="UserName" column="UserName" type="String" />
<property name="Password" column="Password" type="Byte[]" />
<property name="FirstName" column="FirstName" type="String" />
<property name="LastName" column="LastName" type="String" />
<property name="Email" column="Email" type="String" />
<property name="Active" column="Active" type="Boolean" />
<property name="Locked" column="Locked" type="Boolean" />
<property name="LoginFailures" column="LoginFailures" type="int" />
<property name="LockoutDate" column="LockoutDate" type="DateTime" generated="insert" />
<property name="Expired" column="Expired" type="Boolean" generated="insert"/>
<set name="Roles" table="UsersRolesBridge" lazy="false">
<key column="UserKey" />
<many-to-many class="Role"
not-found="exception"
column="RoleKey" />
</set>
</class>
</hibernate-mapping>
Role.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Sample.Persistence" namespace="Sample.Persistence.Model">
<class name="Role" table="Roles">
<id name="RoleKey">
<generator class="identity"/>
</id>
<property name="Name" column="Name" type="String" />
<set name="Users" inverse="true" table="UsersRolesBridge" lazy="false" >
<key column="RoleKey" />
<many-to-many class="User" column="UserKey" />
</set>
</class>
</hibernate-mapping>
我能够通过NHibernate为每个用户检索角色,但是当我去保存新对象时,角色不会保存在Bridge表中。
创建并插入用户没有任何问题。我已经检查过,在调用Session.Save()之前,角色集合(用户的一个字段)正在填充适当的rolekey。
也没有抛出异常。
更新:
添加级联后,仍然没有实际插入M-2-M表。
来自Nhibernate的日志显示如下:
12/22/2010 23:18:11.684 [11] INFO NHibernate.Engine.Cascade
Message: processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: Sample.Persistence.Model.User
Exception:
12/22/2010 23:18:11.686 [11] INFO NHibernate.Engine.Cascade
Message: done processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: Sample.Persistence.Model.User
Exception:
12/22/2010 23:18:11.789 [11] INFO NHibernate.Engine.Cascade
Message: processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: Sample.Persistence.Model.User
Exception:
12/22/2010 23:18:11.792 [11] INFO NHibernate.Engine.Cascade
Message: cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for collection: Sample.Persistence.Model.User.Roles
Exception:
12/22/2010 23:18:11.814 [11] INFO NHibernate.Engine.Cascade
Message: done cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for collection: Sample.Persistence.Model.User.Roles
Exception:
12/22/2010 23:18:11.814 [11] INFO NHibernate.Engine.Cascade
Message: done processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: Sample.Persistence.Model.User
答案 0 :(得分:2)
我通常发现许多问题都是由于没有正确设置级联属性引起的。
尝试在Users.hbm.xml文件中设置的角色上设置cascade =“save-update”属性
<set name="Roles" table="UsersRolesBridge" lazy="false" cascade="save-update">
答案 1 :(得分:2)
您尚未在关系中指定级联。默认情况下,插入,更新和删除不是级联的。更多信息: