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 654035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:26:58+00:00 2026-05-13T22:26:58+00:00

Suppose I have a database like this: This is set up to give role-wise

  • 0

Suppose I have a database like this:

alt text

This is set up to give role-wise menu permissions.

Please note that, User-table has no direct relationship with Permission-table.

Then how should I map this class against the database-tables?

class User
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public bool? IsActive { get; set; }

        public IList<Role> RoleItems { get; set; }
        public IList<Permission> PermissionItems { get; set; }
        public IList<string> MenuItemKeys { get; set; }
    }

This means,

(1) Every user has some Roles.

(2) Every user has some Permissions (depending on to Roles).

(3) Every user has some permitted MenuItemKeys (according to Permissions).

How should my User.hbm.xml look like?

  • 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-13T22:26:58+00:00Added an answer on May 13, 2026 at 10:26 pm

    Roles and Permissions are likely to be accessed a lot in the application. They are very likely to be in the second level cache, which means we can expect to efficiently iterate the User.RoleItems and Role.Permissions.

    This has the advantage that we can generally expect to perform no queries when iterating those collections.

    You could map the classes as follows.

    The properties User.PermissionItems and User.MenuItemKeys are derived from the persistent entities, and thus do not appear in the mappings.

    <class name="User" table="user">
      <id name="ID">
        <generator class="native"/>
      </id>
      <property name="Name"/> 
      <property name="Username"/>
      <property name="Password"/>
      <property name="IsActive"/>
    
      <bag name="RoleItems" table="userrole" lazy="true">
        <key column="userid" />
        <many-to-many class="Role" column="roleid"/>
      </bag>
    </class>
    
    <class name="Role" table="role">
      <id name="ID">
        <generator class="native"/>
      </id>
      <property name="RoleName"/>
      <property name="IsActive"/>
    
      <bag name="Permissions" table="permission">
        <key column="roleid" />
        <one-to-many class="Permission"/>
      </bag>
    </class>
    
    <class name="Permission" table="permission">
      <id name="ID">
        <generator class="native"/>
      </id>
      <property name="MenuItemKey"/>
    </class>
    

    I would make the 2 additional lists you had on User into derived enumerations. If they were lists, there is no unambiguous way to insert into them since you cannot know to which role the value applies. Also, a Role is not owned by a User.

    Update: now using Diego’s improved version of these properties.

    class User
    {
        public virtual IEnumerable<Permission> PermissionItems
        {
            get {
                return RoleItems.SelectMany(role => role.PermissionItems);
            }
        }
    
        public virtual IEnumerable<string> MenuItemKeys
        {
            get {
                return RoleItems.SelectMany(role => role.PermissionItems,
                            (role, permission) => permission.MenuItemKey);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.