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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:21:01+00:00 2026-05-23T18:21:01+00:00

Hello I have two domain classes as following class Users { String password String

  • 0

Hello I have two domain classes as following

class Users {

    String password
    String firstName
    String lastName
    String emailAddress
    String username
    Company company
.....
    static hasMany = [projects:Projects];
}

Another class

class Projects {
    String projectName
    String description
    Users projectLead
    Date dateCreated
    Date lastUpdated
    static belongsTo = Users
}

These classes obviously has one to many relationship but now I want to change it to many to many relationship by adding “ProjectMembership” class but the problem I have is that my application has already gone into production and there are people who are already using the app. In such a case they already have one user->many projects in the the db. In such a case how can I migrate this existing data and change my prod app to have m2m relationship which will looks like following.

class Users {

    String password
    String firstName
    String lastName
    String emailAddress
    String username
    Company company
.....
    static hasMany = [projectMemberships:ProjectMemberships];
}

Another class

class Projects {
    String projectName
    String description
    Users projectLead
    Date dateCreated
    Date lastUpdated
    static hasMany = [projectMemberships:ProjectMemberships];
}

and

class ProjectMemberships{
    Users u
    Projects p
}
  • 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-23T18:21:02+00:00Added an answer on May 23, 2026 at 6:21 pm

    This is best done with a migration tool like Liquibase, and the http://grails.org/plugin/database-migration plugin is probably your best be in Grails since it uses Liquibase and is tightly integrated with GORM. But this one’s easy enough to do by hand.

    I wouldn’t use hasMany since you can easily manage everything from the ProjectMemberships class, so your Users and Projects classes would be

    class Users {
       String password
       String firstName
       String lastName
       String emailAddress
       String username
       Company company
    .....
    }
    

    and

    class Projects {
       String projectName
       String description
       Date dateCreated
       Date lastUpdated
    }
    

    I’d go with a ProjectMemberships class that uses a composite key, which requires that it implement Serializable and have a good hashCode and equals:

    import org.apache.commons.lang.builder.HashCodeBuilder
    
    class ProjectMemberships implements Serializable {
       Users u
       Projects p
    
       boolean equals(other) {
          if (!(other instanceof ProjectMemberships)) {
             return false
          }
    
          other.u?.id == u?.id && other.p?.id == p?.id
       }
    
       int hashCode() {
          def builder = new HashCodeBuilder()
          if (u) builder.append(u.id)
          if (p) builder.append(p.id)
          builder.toHashCode()
       }
    
       static ProjectMemberships get(long userId, long projectId) {
          find 'from ProjectMemberships where u.id=:userId and p.id=:projectId',
             [userId: userId, projectId: projectId]
       }
    
       static ProjectMemberships create(Users u, Projects p, boolean flush = false) {
          new ProjectMemberships(u: u, p: p).save(flush: flush, insert: true)
       }
    
       static boolean remove(Users u, Projects p, boolean flush = false) {
          ProjectMemberships instance = ProjectMemberships.findByUsersAndProjects(u, p)
          if (!instance) {
             return false
          }
    
          instance.delete(flush: flush)
          true
       }
    
       static void removeAll(Users u) {
          executeUpdate 'DELETE FROM ProjectMemberships WHERE u=:u', [u: u]
       }
    
       static void removeAll(Projects p) {
          executeUpdate 'DELETE FROM ProjectMemberships WHERE p=:p', [p: p]
       }
    
       static mapping = {
          id composite: ['p', 'u']
          version false
       }
    }
    

    Use ProjectMemberships.create() to add a relationship between a user and a project, and ProjectMemberships.remove() to remove it.

    Run grails schema-export to see the updated DDL (it’ll be in target/ddl.sql). Run the create table statement for the project_memberships table, e.g.

    create table project_memberships (
       p_id bigint not null,
       u_id bigint not null,
       primary key (p_id, u_id)
    )
    

    Then populate it with this SQL (depending on your database you might need a slightly different syntax):

    insert into project_memberships(p_id, u_id) select id, project_lead_id from projects
    

    and finally drop the project_lead_id column from the projects table.

    Of course do a database backup before making any changes.

    You can get a user’s projects with

    def projects = ProjectMemberships.findAllByUsers(user)*.p
    

    and similarly a project’s users with

    def users = ProjectMemberships.findAllByProjects(project)*.u
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes Hello1 and Hello, and I'm calling class Hello1 constructor within
Hello I have two mysql tables one that holds username and password second that
Let's say I have these two arrays: string[] arr1 = new string[2]{Hello, Stack} string[]
Hello I have the following code namespace ConsoleApplication2 { class Program { static void
I have two objects of same type. Class A { String a; List b;
Lets say I have two strings: string s1 = hello; string s2 = hello
Hello I have problem to put together animations of two separate objects to second
Hello I have the following error by git-fsck, which cannot be cleaned by git-gc
Hello I have a string in PHP $string = ...................blah blah blah.................. where the
Hello I have two text inputs which get their values from a datepicker. What

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.