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

The Archive Base Latest Questions

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

The new system I’m starting has several domains that are just join tables for

  • 0

The new system I’m starting has several domains that are just join tables for many-to-many relations. They are all like this example for Spring Security Core. In the case, of Spring Security, a “legacy” database is being mapped, so using a composit key is necessary.

My question is if it’s possible to put a unique constraint on the combination of the fields instead of having to use a composite key and thus having to implement the get and other functions. I’d be quite happy to use the id as the key, but I do need to ensure the records are unique.

In other words, how could I add a unique constraint on secUser + secRole instead of creating a composite key in the following domain?

class SecUserSecRole implements Serializable {

    SecUser secUser
    SecRole secRole

    boolean equals(other) {
        if (!(other instanceof SecUserSecRole)) {
            return false
        }

        other.secUser?.id == secUser?.id &&
            other.secRole?.id == secRole?.id
    }

    int hashCode() {
        def builder = new HashCodeBuilder()
        if (secUser) builder.append(secUser.id)
        if (secRole) builder.append(secRole.id)
        builder.toHashCode()
    }

    static SecUserSecRole get(long secUserId, long secRoleId) {
        find 'from SecUserSecRole where secUser.id=:secUserId and secRole.id=:secRoleId',
            [secUserId: secUserId, secRoleId: secRoleId]
    }

    static SecUserSecRole create(SecUser secUser, SecRole secRole, boolean flush = false) {
        new SecUserSecRole(secUser: secUser, secRole: secRole).save(flush: flush, insert: true)
    }

    static boolean remove(SecUser secUser, SecRole secRole, boolean flush = false) {
        SecUserSecRole instance = SecUserSecRole.findBySecUserAndSecRole(secUser, secRole)
        if (!instance) {
            return false
        }

        instance.delete(flush: flush)
        true
    }

    static void removeAll(SecUser secUser) {
        executeUpdate 'DELETE FROM SecUserSecRole WHERE secUser=:secUser', [secUser: secUser]
    }

    static void removeAll(SecRole secRole) {
        executeUpdate 'DELETE FROM SecUserSecRole WHERE secRole=:secRole', [secRole: secRole]
    }

    static mapping = {
        id composite: ['secRole', 'secUser']
        version false
    }
}
  • 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-26T05:54:59+00:00Added an answer on May 26, 2026 at 5:54 am

    You could do that if you’re allowed to add a new column that’ll be the real primary key, e.g. an autoincrement or a sequence-based column. Then adding the unique constraint would be simple:

    class SecUserSecRole {
    
       SecUser secUser
       SecRole secRole
    
       static constraints = {
          secUser(unique:'secRole')
       }
    }
    

    But I doubt that’s an option 🙂

    Note that Grails 2.0 has support for Hibernate Bags, which addresses the collection performance problems that this SecUserSecRole approach works around.

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

Sidebar

Related Questions

I was just prototyping a new system for deferring certain operations until out of
I want to create a new system that will be built completely using services.
I am designing a new System and I have a lot of Interfaces that
Where a new system concept or new technology is used, one has to build
I'm designing a new system and finding that I am struggling with the structure
We just got new servers for our new system and I want to know
I want to build new system that synchronize data between an appliance and mobile
var grid = new System.Web.UI.WebControls.GridView(); grid.DataSource = from booking in bookings join f in
My company is moving to a new system which has a very poor printing
Pattern is Regex splRegExp = new System.Text.RegularExpressions.Regex(@[\,@,+,\,?,\d,%,.,?,*,&,^,$,(,!,),#,-,_]); All characters work except '-'. Please advise.

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.