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

  • SEARCH
  • Home
  • 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 6708043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:42:41+00:00 2026-05-26T07:42:41+00:00

I have the following entities: User Role and then a join table: UsersRoles (UserId,

  • 0

I have the following entities:

User
Role

and then a join table:

UsersRoles (UserId, RoleId)

and then a RolePermissions table:

RolePermissions(Id, RoleId, ....)

So on the User entity I want to be able to do:

user.Roles
user.RolePermissions

My UserMap looks like:

public class UserMap : ClassMap<User>
{
   public UserMap()
   {

      HasManyToMany(x => x.Roles)
            .Table("RolesUsers")
            .Access.CamelCaseField(Prefix.Underscore)
            .ParentKeyColumn("UserId")
            .ChildKeyColumn("RoleId")
            .Cascade.All()
            .Inverse();
   }

}

Now when I create a new user like:

var user = new User { ... };
user.AddRole(new Role { .... } );

UserRepository.Create(user);

It saves the user, and saves the role, but doesn’t insert into the RolesUsers table?

Also how do I add a property to get all the RolePermissions for a User?

Update

My rolemap has:

 HasManyToMany(x => x.Users)
            .Table("RolesUsers")
            .Access.CamelCaseField(Prefix.Underscore)
            .ParentKeyColumn("RoleId")
            .ChildKeyColumn("UserId")
            .Cascade.All();

My unit test actually passes, but I think it is because I don’t know how to purge the 1st level cache and it isn’t even querying the database with the call for testUser:

     [Test]
    public void ShouldAddARoleToAUser()
    {
        int userId = -1;
        using (var tx = UserRepository.SessionFactory.OpenSession().BeginTransaction())
        {

            var user = FactoryGirl.GetUser();
            user.AddRole(FactoryGirl.GetRole());

            UserRepository.Create(user);

            tx.Commit();

            UserRepository.SessionFactory.OpenSession().Flush();

            userId = user.Id;
        }

        var testUser = UserRepository.Get(userId);

        Assert.IsNotNull(testUser);
        Assert.IsNotNull(testUser.Roles);
        Assert.IsTrue(testUser.Roles.Count == 1);
        Assert.IsTrue(testUser.Roles[0].Id > 0);

    }

Update II

My user.cs looks like:

        private IList<Role> _roles = new List<Role>(); 

        public virtual IList<Role> Roles
        { 
            get { return _roles;  }
        }

        public virtual void AddRole(Role role)
        {
            if(!_roles.Contains(role))
            {
                _roles.Add(role);
            }
        }

Role.cs:

        private IList<User> _users = new List<User>();
        public virtual IList<User> Users
        {
            get { return _users; }
        }

Update III

The strange this is my test passes, but looking at profiler I can see that it doesn’t even hit sql server so even though I am calling Flush, and then doing a Get(userId) it is loading the entity from memory. Also, looking at the other end of the relation (Role), the Role.Users is {} so is that the issue?

  • 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-26T07:42:42+00:00Added an answer on May 26, 2026 at 7:42 am

    If you are using private backing fields for your collections I think you have to map them like this:

    public class UserMap : ClassMap<User>
    {
       public UserMap()
       {
          HasManyToMany(x => x.Roles)
                    .Table("RolesUsers")
                    .Access.AsCamelCaseField(Prefix.Underscore)
                    .ParentKeyColumn("UserId")
                    .ChildKeyColumn("RoleId")
                    .Cascade.All()
                    .Inverse();
       }
    }
    

    Key difference above is .Access.AsCamelCaseField(Prefix.Underscore)

    Edit:

    In addition to this I think you need to simplify your test to something like the following:

    User newUser = new User();
    Role newRole = new Role();
    newUser.AddRole(newRole);
    newRole.AddUser(newUser);
    
    using (NHibernate.ISession session = iSessionFactory.OpenSession())
    {
        using (NHibernate.ITransaction tran = session.BeginTransaction())
        {
            session.Save(newUser);
            tran.Commit();
        }
    }
    

    The above may not be exact but I think you get the idea of what I’m talking about.

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

Sidebar

Related Questions

I have the following database tables: Table1: User UserId Username Table2: Role RoleId Rolename
I have the following two entities: @Entity class Relation{ @ManyToOne private User user; //some
I have the following entities: public class User { public virtual ICollection<Role> Roles {
We have the following two entities with many-to-many association: @Entity public class Role {
I have the following model: And I want the user to be able to
I have the following entities: User Company Organization Users need to be able to
I have following two entities public class User { public int UserId { get;
Say, I have following entities: @Entity public class A { @Id @GeneratedValue private Long
I have the following entities: @Entity public class Owner{ @Id @Column(name = OWNER_ID) @OneToMany()
I have following two entities public class User { [Key] public int Id {

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.