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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:37:36+00:00 2026-05-30T11:37:36+00:00

I have 2 objects, ObjectA and ObjectB . When ObjectA gets created, ObjectB doesn’t

  • 0

I have 2 objects, ObjectA and ObjectB.

When ObjectA gets created, ObjectB doesn’t (and can’t, due to not having the data yet) exist yet.

Once ObjectB is created, it needs to have a corresponding ObjectA attached to it if an appropriate one exists. If there isn’t an appropriate ObjectA, then the new ObjectB simply isn’t connected to one.

So, all ObjectA instances will eventually be attached to an ObjectB, but not all ObjectB instances will have an ObjectA.

Essentially, I’m looking for GORM to build database tables like this:

ObjectA
- Id (NotNull, unique)
- ObjectB_Id[FK: ObjectB.Id] (unique)

ObjectB
- Id (NotNull, unique)

How can I put together the GORM domain classes to do this?

I’ve tried just about every combination of hasOne, belongsTo, raw properties, nullable: true and unique: true constraints I can think of, but I must be missing one. This doesn’t seem like it’s a particularly odd scenario, so there must be some way to accomplish this.

  • 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-30T11:37:37+00:00Added an answer on May 30, 2026 at 11:37 am

    As is often the case, putting my thoughts together into a question lead me to the solution:

    Class ObjectA {
        ObjectB objectB
    
        static constraints = {
            objectB nullable: true, unique: true
        }
    }
    
    Class ObjectB {
        static belongsTo = [objectA: ObjectA]
    
        static constraints = {
            objectA nullable: true
        }
    }
    

    The only catch with this that I can’t seem to get around is that it’s possible to set ObjectB.objectA to an ObjectA that is already associated with an ObjectB. When saving the new association, GORM just doesn’t save the association. No errors are thrown, and pulling the new ObjectB out of the database leaves it’s objectA property unset.

    Adding unique: true to the objectA constraints in the ObjectB class doesn’t help either. Instead of silently failing, an error is thrown indicating that the uniqueness check is failing due to an un-set parameter.

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

Sidebar

Related Questions

I have applications(WinForm) that gets some objects from webservice. After receiving array I transform
I have a class that occasionally gets passed null for File objects. During normal
I have a model which gets its data from a parser object. I'm thinking
I have a complex map, which doesn't need locking for the frequent gets and
General Problem I have some objects that have some variables that are not known
I have a mapkit application, where I've created a custom callout view. I can't
I have a hibernate object that gets detached and transferred to a thick java
I have a code sample that gets a SEL from the current object, SEL
I have a simple object that get's geocoding data from the Google Maps API
Suppose I have a design like this: Object GUI has two objects: object aManager

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.