You are very close to your solution. You need to turn on the cascade feature. By default, the cascade is "none" so in effect, none of the changes to your collection will be persisted. However, keep in mind that cascading "deletions" in many-to-many relationships will probably produce unexpected results. If you delete a User, do you really want the Role object to be deleted? Probably not, you probably just want the association.
Change your mapping file for user like so:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="tik2005.tik2005.user, tik2005" table="tik2005_users">
<id name="userId" type="integer" unsaved-value="0">
<generator class="identity" />
</id>
<property name="Email" type="String" length="40" />
<property name="Password" type="String" length="20" />
<property name="Name" type="String" length="40" />
<bag name="Roles"
cascade="save-update"
table="tik2005_userroles">
<key column="userId" />
<many-to-many column="roleId" class="tik2005.tik2005.Roles, tik2005" />
</bag>
</class>
</hibernate-mapping>
the addition of the cascade attrubute tells NHibernate to persist additions, and updates to the "Roles" collection. Additionally, deleting a User will automatically delete all "UserRoles" from the association table.
In order to have NHibernate delete all instances of the user assocations from the Role side of things, you need to add a collection mapping to the role object like so:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="tik2005.tik2005.Roles, tik2005" table="tik2005_roles">
<id name="roleId" type="integer" unsaved-value="0">
<generator class="identity" />
</id>
<property name="roleName" type="String" length="50"/>
</class>
<bag name="Users"
inverse="true"
cascade="save-update"
table="tik2005_userroles">
<key column="roleID" />
<many-to-many column="userId" class="tik2005.tik2005.user, tik2005" />
</bag>
</hibernate-mapping>
With the inverse="true" attribute, you are telling NHibernate to ignore this end of the association, and use the "User.Roles" collection to manage changes to the association.
If you really want to have NHibernate delete associations, you will probably need to refactor your code, create an association class, and use a one-to-many relationships. In that instance you can use cascade="all-delete-orphan" to delete the associations as you want.
HTH,
-devon