NHibernate Many-to-Many Mapping not working
I have a Nhibernate mapping file for a simple user/role mapping.
Here are the mapping files:
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" />
开发者_Python百科 <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>
I am able to retrieve roles for each user via NHibernate but when I go to save a new object, the roles are not saved in the Bridge table.
The user is created and insert with no issues. I've checked that the Role collection, a field on the user, is being populated with the proper rolekey before the Session.Save() is called.
There is no exception thrown as well.
UPDATE:
After adding a cascade, there was still no actual insert taking place to the M-2-M table.
The logs from Nhibernate showed the following:
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
You haven't specified a cascade on the relationship. By default, inserts, updates, and deletes are not cascaded. More information here:
http://ayende.com/Blog/archive/2006/12/02/nhibernatecascadesthedifferentbetweenallalldeleteorphansandsaveupdate.aspx
I generally find that many to many issues are caused by not setting the cascade properties properly.
Try setting the cascade = "save-update" attribute on the Roles set in the Users.hbm.xml file
<set name="Roles" table="UsersRolesBridge" lazy="false" cascade="save-update">
精彩评论