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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:06:06+00:00 2026-05-18T01:06:06+00:00

So, we have a many-to-many relationship between Customer and Role, set up as: Customer

  • 0

So, we have a many-to-many relationship between Customer and Role, set up as:

Customer {
  static hasMany = [roles: Role]
}

Role {
  static hasMany = [customer: Customer]
  static belongsTo = Customer
}

The Role object has only a name and a set of permissions. We don’t want to cascade saves from Customer -> Role, as Role should only get modified directly.

I added:

static mapping = {
  roles cascade: 'none'
}

but, whenever, I create a customer, the role table gets updated as well. Nothing changes, except that the version number is incremented.

Am I missing something else that needs to be set … is there a bug with how many-to-many relationships and cascades are set in Grails … or is there some other way I can prevent roles from being updated every time?

  • 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-18T01:06:07+00:00Added an answer on May 18, 2026 at 1:06 am

    I usually map the join table as a domain class to avoid this issue and others (collection loading performance, optimistic locking errors, etc.) This involves removing the hasMany and belongsTo and creating a CustomerRole domain class:

    import org.apache.commons.lang.builder.HashCodeBuilder
    
    class CustomerRole implements Serializable {
    
       Customer customer
       Role role
    
       boolean equals(other) {
          if (!(other instanceof CustomerRole)) {
             return false
          }
    
          other.customer?.id == customer?.id &&
             other.role?.id == role?.id
       }
    
       int hashCode() {
          def builder = new HashCodeBuilder()
          if (customer) builder.append(customer.id)
          if (role) builder.append(role.id)
          builder.toHashCode()
       }
    
       static CustomerRole get(long customerId, long roleId) {
          find 'from CustomerRole where customer.id=:customerId and role.id=:roleId',
             [customerId: customerId, roleId: roleId]
       }
    
       static CustomerRole create(Customer customer, Role role, boolean flush = false) {
          new CustomerRole(customer: customer, role: role).save(flush: flush, insert: true)
       }
    
       static boolean remove(Customer customer, Role role, boolean flush = false) {
          CustomerRole instance = CustomerRole.findByCustomerAndRole(customer, role)
          instance ? instance.delete(flush: flush) : false
       }
    
       static void removeAll(Customer customer) {
          executeUpdate 'DELETE FROM CustomerRole WHERE customer=:customer', [customer: customer]
       }
    
       static void removeAll(Role role) {
          executeUpdate 'DELETE FROM CustomerRole WHERE role=:role', [role: role]
       }
    
       static mapping = {
          id composite: ['customer', 'role']
          version false
          table 'customer_roles'
       }
    }
    

    The mapping block configures the generated DDL so it’s the same as what you have now, so you won’t need to make any database changes. The static helper methods aren’t required but are convenient to hide the process of granting and revoking roles.

    You will need to change you code. Since there’s no hasMany, you can’t use customer.addToRoles(...). Instead to grant a role just create a new CustomerRole instance using the create method and to revoke, delete the instance using the remove method.

    The updated Role class would be

    class Role {
    }
    

    and the updated Customer class would be

    class Customer {
       Set<Role> getRoles() {
          CustomerRole.findAllByUser(this).collect { it.role } as Set
       }
    }
    

    This has a convenience method getRoles() which mimics the roles collection that’s created for you by the hasMany since you will still need an easy way to access a customer’s granted roles.

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

Sidebar

Related Questions

I have a setup that has a many to many relationship between customer and
I have a Many to Many relationship between User and Role. They are set
I have a Hash Map (many-to-one relationship between texts and boolean values): name flag
I have an intermediate table that defines many-to-many relationship between, for instance, Customer and
Let's say we have one to many relationship between Customer and Phone.. class Customer{
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
I have a many-to-many relationship between users and groups and I have a table
I have a many-to-many relationship between User s and Task s. I want the
Is it possible to have a many to many relationship between two tables, and
I have a one-to-many relationship between two classes for this situation. I have a

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.