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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:41:47+00:00 2026-06-02T15:41:47+00:00

I have come accross something that at first view seems to be a bug

  • 0

I have come accross something that at first view seems to be a bug in Gorm (grails 1.3.7). I thought I’d post the problem here before going for the jira issue, in case I’m getting it wrong.

Here’s the issue:

I have one domain class, it refers to a parent object of the same class, and also can have a pointer to an alias, also of the same domain class.

This is the domain class:

class Mydomain {
    String name
    Mydomain alias
    Mydomain parent
    Mydomain foo

    static constraints = {
        parent(nullable: true)
        alias(nullable: true)
        foo(nullable: true)
    }
}

If I execute the following script:

Mydomain.list()*.delete()
def one=new Mydomain(name:'one').save()
new Mydomain(name:'two', parent:one).save()

Mydomain.list().each{
    println it.name
    println "parent:${it.parent?.name}"
    println "alias:${it.alias?.name}"
    println "foo:${it.foo?.name}"
    println "============================"
}    

I get the following result:

one
parent:null
alias:two
foo:null
============================
two
parent:one
alias:null
foo:null
============================

This means that when I set two.parent=one, gorm goes and sets one.alias=two.

I guess that gorm does this because it infers that there is a bidirectional relationship between one and two, and then sets the first property of class Mydomain of object one to the reference of two.

I can see this sort of behaviour working with bidirectional relationships between Author and Book (assuming that an author only writes one book of course), but in my case this is dangerous, becaus gorm goes and overwrites a relationship that has nothing to do with this.

So my question would be, how do I tell GORM to treat this as unidirectional nullable relationships?

Thanks for any ideas

Update: Business case

This is the business case that I am trying to model.
A company has a name, and can have subsidiaries, warehouses, local offices, etc. This is modeled with the help of the parent relationship, effectively creating a tree of Companies:

class Company {
    String name
    Company parent
}

Now, the representation of this organizational structure is not the same for everyone, some people may not care about the office level, or event the group level. This is why a company can have an alias in a parallel representation. It is still the same company, can have a different name, but you would like to be able to navigate between these parallel tree organizations via the alias relationship.

class Company {
    String name
    Company parent
    Company alias
}

So yes, there are multiple relationships to self, but they have very distinct meanings, and I understand that GORM uses the most frequent simple case as a default, but I am really trying everything I can in the DSL to instruct GORM to do the right thing, but unsuccessfully.

The conventional way of doing this would be to add a bidirectional one-to-many relationship, with a mappedBy directive:

class Company {
    String name
    Company alias

    static belongsTo=[parent:Company]
    static hasMany=[children:Company]
    static mappedBy=[children:'parent']
}

But in many GORM articles this idiom is discouraged, because of severe performance issues. That is why I want an unidirectional relationship

Company ---(parent)---> Company
  • 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-06-02T15:41:51+00:00Added an answer on June 2, 2026 at 3:41 pm

    After posting a JIRA issue
    http://jira.grails.org/browse/GRAILS-9062?focusedCommentId=70641#comment-70641

    and some exchanges in the mailing list,

    http://markmail.org/message/xcmcclr2lv7ecrbn

    The solution was suggested by Graeme Rocher.

    Just add the following mappedBy closure

    static mappedBy = [
        parent: 'null',
        alias:'null',
        foo:'null'
    ]
    

    This will instruct GORM not to make the assumption that a bidirectional relationship is needed.

    Thanks to all for your expert help!

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

Sidebar

Related Questions

I have just come across something that is quite strange and yet I haven't
I'm porting an application from VB6 to VB.NET and have come across something that
I am working on some legacy code and have come across something that I'm
So I am attempting to learn C++ and I have come across something that
Using php5.2 and MySQL 4.1.22 I've come across something that, at first, appeared simple
Sometimes I come across this problem where you have a set of functions that
I am trying to configure TFS 2010 reporting and have come accross the following
I am trying to learn how NSCoding works, but seem to have come accross
I have come across some strange behaviour, and I'm assuming a bug in Firefox,
I'm new to PHP (and web development in general) and have come across something

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.