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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:09:13+00:00 2026-05-27T03:09:13+00:00

I have two following domains: User { UserData userData } UserData { static belongsTo

  • 0

I have two following domains:

User {
   UserData userData
}

UserData {
   static belongsTo = [user: User]
}

and at some point I want to merge two users into one. I mean delete one instance of User and attache userData to another user.

I’ve tried:

User zombieUser
User liveUser

UserData data = zombieUser.userData
zombieUser.delete()
liveUser.userData = data
userData.user = liveUser
userData.save()
liveUser.save()

Actually I’ve tried different variants, different order, seems that all possible ways. But it always fails with an exceptions. Current code will fail with:

deleted object would be re-saved by cascade (remove deleted object from associations): [UserData#1]

If i’ve put zombie.delete() to bottom, after *.save(), I will get:

Field error in object 'User' on field 'userData': rejected value [UserData : 1]; codes ... default message [Property [{0}] of class [{1}] with value [{2}] must be unique]

Are there any way to reconnect existing object from one object to another?


Working code:

UserData userData = zombieUser.userData

userData.user = null
zombieUser.userData = null
zombieUser.save(flush: true)
userData.save()

liveUser.userData = userData
userData.user = liveUser
liveUser.save()
userDate.save(flush: true)
  • 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-27T03:09:13+00:00Added an answer on May 27, 2026 at 3:09 am

    The problem is that because of your belongsTo declaration, and according to cascading rules, userData is deleted from the database as soon as zombieUser is deleted. When liveUser.save() is called, userData would be saved again. By the way, you call to userData.save() is not needed as again, because of this belongsTo declaration, liveUser.save() is cascaded to userData.

    I see two options for solving your problem:

    1. Perform a deep copy of userData to a new UserData object, and attach this object to liveUser, like this:

      withTransaction {
        UserData data = new UserData(prop: zombieUser.userData.prop)
        liveUser.userData = data
        zombiUser.delete()
        liveUser.save()
      }
      

      The old UserData object will be deleted by cascade when deleting zombieUser and a new one will be created when saving liveUser.

    2. Remove belongsTo from userData. You won’t benefit of cascading anymore, and will have to manage saving userData yourself, and make sure user.userData is deleted from the database when no user reference it (don’t forget to make transactions for that, as always when you have several call to save or delete in your method).

    Which approach you choose depends on how coupled your objects are. Typically, if they are very coupled, you will benefit of cascading (through belongsTo) and may go for 1. If they are not very coupled, (i.e. there may be several users with the same userData), then belongsTo is wrong and you should choose approach 2.

    The concepts presented in GORM gotchas will greatly help you if you have a GORM model with complicated dependencies.

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

Sidebar

Related Questions

I have the following two projects in in Flex Builder 3: One AS3 library
I have the following two classes, one inherits from the other Class A{ void
I have two following classes: public class User { public virtual Guid Id {
If I have two domain names: altcognito.com and say I've got the other following
I am using VB.NET with LINQ to MS SQL. I have two following tables.
I have two tables with the following columns: table1: id, agent_name, ticket_id, category, date_logged
I have the following two columns: SELECT b.ip_address AS IP ,b.mask AS MASK FROM
I have the following two tables, affiliates and referrers. affiliates Table id loginid 3
I have two dictionaries of the following structure: a) dict1 = {'a':[ [1,2], [3,4]
I have the following two tables: Customer { int Id int Name } Bills

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.