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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:38:55+00:00 2026-05-11T00:38:55+00:00

I have a scenario in a system which I’ve tried to simplify as best

  • 0

I have a scenario in a system which I’ve tried to simplify as best as I can. We have a table of (lets call them) artefacts, artefacts can be accessed by any number of security roles and security roles can access any number of artefacts. As such, we have 3 tables in the database – one describing artefacts, one describing roles and a many-to-many association table linking artefact ID to Role ID.

Domain wise, we have two classes – one for a role and one for an artefact. the artefact class has an IList property that returns a list of roles that can access it. (Roles however do not offer a property to get artefacts that can be accessed).

As such, the nhibernate mapping for artefact contains the following;

<bag name='AccessRoles' table='ArtefactAccess' order-by='RoleID'      lazy='true' access='field.camelcase-underscore' optimistic-lock='false'>     <key column='ArtefactID'/>     <many-to-many class='Role' column='RoleID'/> </bag> 

This all works fine and if I delete an artefact, the association table is cleaned up appropriately and all references between the removed artefact and roles are removed (the role isn’t deleted though, correctly – as we don’t want orphans deleted).

The problem is – how to delete a role and have it clear up the association table automatically. If I presently try to delete a role, I get a reference constraint as there are still entries in the association table for the role. The only way to successfully delete a role is to query for all artefacts that link to that role, remove the role from the artefact’s role collection, update the artefacts and then delete the role – not very efficient or nice, especially when in the un-simplified system, roles can be associated with any number of other tables/objects.

I need to be able to hint to NHibernate that I want this association table cleared whenever I delete a role – is this possible, and if so – how do I do it?

Thanks for any help.

  • 1 1 Answer
  • 3 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. 2026-05-11T00:38:56+00:00Added an answer on May 11, 2026 at 12:38 am

    Since I was looking for this answer and found this thread on google (without an answer) I figured I’d post my solution to this. With three tables: Role, RolesToAccess(ManyToMany), Access.

    Create the following mappings: Access:

    <bag name='Roles' table='RolesToAccess' cascade='none' lazy='false'>       <key column='AccessId' />       <many-to-many column='AccessId' class='Domain.Compound,Domain' />     </bag>  <bag name='RolesToAccess' cascade='save-update' inverse='true' lazy='false'>       <key column='AccessId' on-delete='cascade' />       <one-to-many class='Domain.RolesToAccess,Domain' />     </bag> 

    Roles:

    <bag name='Accesses' table='RolesToAccess' cascade='none' lazy='false'>       <key column='RoleId' />       <many-to-many column='RoleId' class='Domain.Compound,Domain' />     </bag>  <bag name='RolesToAccess' cascade='save-update' inverse='true' lazy='false'>       <key column='RoleId' on-delete='cascade' />       <one-to-many class='Domain.RolesToAccess,Domain' />     </bag> 

    As mentioned above you can make the RolesToAccess properties protected so they don’t pollute your model.

    • 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.