Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3214442
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:03:41+00:00 2026-05-17T15:03:41+00:00

I have these 2 objects in NHibernate forming a many to many relationship: User:

  • 0

I have these 2 objects in NHibernate forming a many to many relationship:

User:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Providers" namespace="Providers.Objects">

  <class name="User" table="Users">

    <id name="UserId" type="int">
      <generator class="native" />
    </id>

    <many-to-one name="Application" column="ApplicationId" cascade="none" />    

    <property name="UserName" type="string" />
    <property name="LoweredUserName" type="string" />
    <property name="MobileAlias" type="string" />
    <property name="IsAnonymous" type="bool" />
    <property name="LastActivityDate" type="DateTime" />

    <bag name="Roles" table="UsersInRoles" lazy="true" cascade="none" >
      <key column="UserId"></key>
      <many-to-many class="Role" column="RoleId"></many-to-many>
    </bag>

  </class>

</hibernate-mapping>

And Role:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Providers" namespace="Providers.Objects">

  <class name="Role" table="Roles">

    <id name="RoleId" type="int">
      <generator class="native" />
    </id>

    <many-to-one name="Application" column="ApplicationId" class="Application" cascade="none" />    

    <property name="RoleName" type="string" />
    <property name="LoweredRoleName" type="string" />
    <property name="Description" type="string" />

    <bag name="Users" table="UsersInRoles" lazy="true" inverse="true" cascade="none" >
      <key column="RoleId"></key>
      <many-to-many class="User" column="UserId"></many-to-many>
    </bag>

  </class>

</hibernate-mapping>

Let’s say the role backupoperator has some users in it. If I try to remove one of the users from the role instance, like:

var backupoperator = GetRoleByName(session, app.ApplicationId, "backupoperator");
backupoperator.Users.RemoveAt(0);
session.Update(backupoperator);
transaction.Commit();

It doesn’t work 🙁 The association remains unchanged in the database. When I try the opposite (remove a role from a user object and updating the user object), it works.
Is it because of the inverse attribute in the NHibernate mapping?

How to accomplish what I am trying to do? (remove a user from a role, updating the role and having that persisted)?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:03:42+00:00Added an answer on May 17, 2026 at 3:03 pm

    When you write inverse="true" you are telling NHibernate that the other side maintains the relationship.

    Therefore, you have to remove the Role from the User’s Roles collection if you want your change persisted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these container objects (let's call them Container) in a list. Each of
I have a bunch of objects in a flat structure. These objects have an
I have a JSON array with ActiveRecord objects. These objects can be reconstructed using
I have a class that creates several IDisposable objects, all of these objects are
I have some model objects I'm using in my Java client application. Later these
I have an XML that I want to load to objects, manipulate those objects
Noob question. I have this situation where I have these objects: class Address {
I have some business objects which use: Web.Configuration.WebConfigurationManager.AppSettings.Item(SomeSetting) Now that I'm breaking those objects
I have a custom object that implements INotifyPropertyChanged. I have a collection of these
I'm using S#arpArchitecture (ASP.NET MVC and Fluent NHibernate). I have an entity as follows:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.