Quantcast

grails 1.3.7 - GORM misinterpretation of nullable reflective many-to-one relationship (possible bug?)

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

grails 1.3.7 - GORM misinterpretation of nullable reflective many-to-one relationship (possible bug?)

Luis Muniz-2
Sorry for crossposting (with stack exchange), but I feel this one is justified, as it is a possible bug inside gorm, and in my opinion a dangerous one.

Feel free to educate me otherwise:

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

   
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 it
.alias?.name
    println
"============================"
}

I get the following result:

one
two
============================
two
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


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: grails 1.3.7 - GORM misinterpretation of nullable reflective many-to-one relationship (possible bug?)

Luis Muniz-2
update, in grails 2.0.1 I have the same behaviour.

On Wed, Apr 25, 2012 at 9:38 AM, Luis Muniz <[hidden email]> wrote:
Sorry for crossposting (with stack exchange), but I feel this one is justified, as it is a possible bug inside gorm, and in my opinion a dangerous one.

Feel free to educate me otherwise:

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

   
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 it
.alias?.name
    println
"============================"
}

I get the following result:

one
two
============================
two
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



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: grails 1.3.7 - GORM misinterpretation of nullable reflective many-to-one relationship (possible bug?)

Luis Muniz-2
bump.

Revised example:

class Mydomain {
   
String name
   
Mydomain alias
   
Mydomain parent
   
Mydomain foo

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

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
"============================"
}    


Resulting in:

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



On Wed, Apr 25, 2012 at 12:18 PM, Luis Muniz <[hidden email]> wrote:
update, in grails 2.0.1 I have the same behaviour.


On Wed, Apr 25, 2012 at 9:38 AM, Luis Muniz <[hidden email]> wrote:
Sorry for crossposting (with stack exchange), but I feel this one is justified, as it is a possible bug inside gorm, and in my opinion a dangerous one.

Feel free to educate me otherwise:

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

   
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 it
.alias?.name
    println
"============================"
}

I get the following result:

one
two
============================
two
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




Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: grails 1.3.7 - GORM misinterpretation of nullable reflective many-to-one relationship (possible bug?)

Luis Muniz-2
Maybe to clarify the domain model, I used Burt Beckwith's GORM idioms to remove the collection in the hasMany part of the relationship.

The conventional GORM implementation i guess should look something like this:

class Mydomain3 {
    String name

    static belongsTo = [
        parent: Mydomain3,
        alias: Mydomain3,
        foo: Mydomain3
    ]
    static hasMany = [
        children: Mydomain3,
        aliases: Mydomain3,
        foos: Mydomain3
    ]
    static mappedBy = [
        children: 'parent',
        aliases: 'alias',
        foos: 'foo'
    ]

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

The fact that the target of there are multiple relationships confuses GORM apparently, so that when you assign parent in objetc one, GORM assignes alias in object two.



On Thu, Apr 26, 2012 at 9:02 AM, Luis Muniz <[hidden email]> wrote:
bump.

Revised example:

class Mydomain {
   
String name
   
Mydomain alias
   
Mydomain
parent
   
Mydomain foo

   
static constraints = {

        parent
(nullable: true)
       
alias(nullable: true)
        foo
(nullable: true)
   
}
}

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
"============================"
}    


Resulting in:

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



On Wed, Apr 25, 2012 at 12:18 PM, Luis Muniz <[hidden email]> wrote:
update, in grails 2.0.1 I have the same behaviour.


On Wed, Apr 25, 2012 at 9:38 AM, Luis Muniz <[hidden email]> wrote:
Sorry for crossposting (with stack exchange), but I feel this one is justified, as it is a possible bug inside gorm, and in my opinion a dangerous one.

Feel free to educate me otherwise:

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

   
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 it
.alias?.name
    println
"============================"
}

I get the following result:

one
two
============================
two
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





Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: grails 1.3.7 - GORM misinterpretation of nullable reflective many-to-one relationship (possible bug?)

ideasculptor


On Thu, Apr 26, 2012 at 12:24 AM, Luis Muniz <[hidden email]> wrote:
Maybe to clarify the domain model, I used Burt Beckwith's GORM idioms to remove the collection in the hasMany part of the relationship.

The conventional GORM implementation i guess should look something like this:

class Mydomain3 {
    String name

    static belongsTo = [
        parent: Mydomain3,
        alias: Mydomain3,
        foo: Mydomain3
    ]
    static hasMany = [
        children: Mydomain3,
        aliases: Mydomain3,
        foos: Mydomain3
    ]
    static mappedBy = [
        children: 'parent',
        aliases: 'alias',
        foos: 'foo'

    ]

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

The fact that the target of there are multiple relationships confuses GORM apparently, so that when you assign parent in objetc one, GORM assignes alias in object two.


Given the lack of response to your emails, I'd file a jira because this sure looks like a pretty significant bug to me.  I'm curious whether the same behaviour is exhibited if they aren't self-referential relationships, but you still have multiple uni-directional relationships to a second class.  If that works correctly, then it is at least a bug only in a relatively rare case of maintaining multiple recursive unidirectional relationships. That doesn't help you any, since it will likely actually slow a fix if it is a rare corner case.  Does it work correctly when you've got bidirectional relationships for all of them, as in your example above, or is it still broken in some way?  I don't have anything to offer you by way of a fix or workaround, I'm just curious about the extent of the bug.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: grails 1.3.7 - GORM misinterpretation of nullable reflective many-to-one relationship (possible bug?)

Graeme Rocher-4
Administrator
In reply to this post by Luis Muniz-2
Currently you use mappedBy to control whether an association is
linked. If you don't want an association linked you can do something
like

static mappedBy = [parent:'null']

Cheers

On Thu, Apr 26, 2012 at 9:24 AM, Luis Muniz <[hidden email]> wrote:

> Maybe to clarify the domain model, I used Burt Beckwith's GORM idioms to
> remove the collection in the hasMany part of the relationship.
>
> The conventional GORM implementation i guess should look something like
> this:
>
> class Mydomain3 {
>     String name
>
>     static belongsTo = [
>         parent: Mydomain3,
>         alias: Mydomain3,
>         foo: Mydomain3
>     ]
>     static hasMany = [
>         children: Mydomain3,
>         aliases: Mydomain3,
>         foos: Mydomain3
>     ]
>     static mappedBy = [
>         children: 'parent',
>         aliases: 'alias',
>         foos: 'foo'
>
>     ]
>
>     static constraints = {
>         parent(nullable: true)
>         alias(nullable: true)
>         foo(nullable: true)
>     }
> }
>
> The fact that the target of there are multiple relationships confuses GORM
> apparently, so that when you assign parent in objetc one, GORM assignes
> alias in object two.
>
>
>
>
> On Thu, Apr 26, 2012 at 9:02 AM, Luis Muniz <[hidden email]> wrote:
>>
>> bump.
>>
>> Revised example:
>>
>> class Mydomain {
>>
>>     String name
>>     Mydomain alias
>>     Mydomain
>>  parent
>>
>>     Mydomain foo
>>
>>     static constraints = {
>>
>>
>>         parent(nullable: true)
>>         alias(nullable: true)
>>
>>
>>         foo(nullable: true)
>>     }
>>
>> }
>>
>> 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 "============================"
>> }
>>
>>
>> Resulting in:
>>
>> one
>>
>> parent:null
>> alias:two
>> foo:null
>>
>> ============================
>> two
>> parent:one
>> alias:null
>>
>> foo:null
>> ============================
>>
>>
>>
>> On Wed, Apr 25, 2012 at 12:18 PM, Luis Muniz <[hidden email]> wrote:
>>>
>>> update, in grails 2.0.1 I have the same behaviour.
>>>
>>>
>>> On Wed, Apr 25, 2012 at 9:38 AM, Luis Muniz <[hidden email]> wrote:
>>>>
>>>> Sorry for crossposting (with stack exchange), but I feel this one is
>>>> justified, as it is a possible bug inside gorm, and in my opinion a
>>>> dangerous one.
>>>>
>>>> Feel free to educate me otherwise:
>>>>
>>>> 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
>>>>
>>>>     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 it.alias?.name
>>>>     println "============================"
>>>>
>>>> }
>>>>
>>>> I get the following result:
>>>>
>>>> one
>>>> two
>>>> ============================
>>>> two
>>>> 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
>>>>
>>>>
>>>
>>
>



--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: grails 1.3.7 - GORM misinterpretation of nullable reflective many-to-one relationship (possible bug?)

Luis Muniz-2
In reply to this post by ideasculptor
Hi Samuel,

When I use bidirectional by-the-book relationships the issue does not come up. However using these is dangerous in case of big collections, so it is not a thing that I'd like to do. I would have to modify my domain model if I don't have a solution.

I have created a JIRA after talking to guys in #grails :

http://jira.grails.org/browse/GRAILS-9062



On Thu, Apr 26, 2012 at 10:57 AM, Samuel Gendler <[hidden email]> wrote:


On Thu, Apr 26, 2012 at 12:24 AM, Luis Muniz <[hidden email]> wrote:
Maybe to clarify the domain model, I used Burt Beckwith's GORM idioms to remove the collection in the hasMany part of the relationship.

The conventional GORM implementation i guess should look something like this:

class Mydomain3 {
    String name

    static belongsTo = [
        parent: Mydomain3,
        alias: Mydomain3,
        foo: Mydomain3
    ]
    static hasMany = [
        children: Mydomain3,
        aliases: Mydomain3,
        foos: Mydomain3
    ]
    static mappedBy = [
        children: 'parent',
        aliases: 'alias',
        foos: 'foo'

    ]

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

The fact that the target of there are multiple relationships confuses GORM apparently, so that when you assign parent in objetc one, GORM assignes alias in object two.


Given the lack of response to your emails, I'd file a jira because this sure looks like a pretty significant bug to me.  I'm curious whether the same behaviour is exhibited if they aren't self-referential relationships, but you still have multiple uni-directional relationships to a second class.  If that works correctly, then it is at least a bug only in a relatively rare case of maintaining multiple recursive unidirectional relationships. That doesn't help you any, since it will likely actually slow a fix if it is a rare corner case.  Does it work correctly when you've got bidirectional relationships for all of them, as in your example above, or is it still broken in some way?  I don't have anything to offer you by way of a fix or workaround, I'm just curious about the extent of the bug.



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: grails 1.3.7 - GORM misinterpretation of nullable reflective many-to-one relationship (possible bug?)

ideasculptor
In reply to this post by Graeme Rocher-4


On Thu, Apr 26, 2012 at 2:58 AM, Graeme Rocher <[hidden email]> wrote:
Currently you use mappedBy to control whether an association is
linked. If you don't want an association linked you can do something
like

static mappedBy = [parent:'null']

He wants it linked.  The problem is that GORM is making assumptions about other links of the same type that are utterly invalid.  It is assuming that since  There is a property of the correct type on each side of the link, that it can assign things bidirectionally when that isn't actually the case. There are two properties of the same type, which also happens to be the type of the class being mapped.  Assigning a reference to object1 into the 2nd property of object2, causes the first property in object1 to be assigned to object2, which is just totally wrong.  It isn't a bidirectional relationship and assigning into one entity should have no impact on the other entity at all.  Gorm appears to be making an assumption about bidirectionality based on the existence of properties that match by type rather than an explicit mapping.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: grails 1.3.7 - GORM misinterpretation of nullable reflective many-to-one relationship (possible bug?)

Luis Muniz-2
In reply to this post by Graeme Rocher-4
Wow!!

grails is awesome Graeme!

I really did not think that I was going to be bailed out by a GORM directive here.

I added the following (although intellij idea complains about missing hasMany directives):

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


And now I get the following result:

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

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

Exactly as expected. Fantastic!!!

You guys rock

On Thu, Apr 26, 2012 at 11:58 AM, Graeme Rocher <[hidden email]> wrote:
Currently you use mappedBy to control whether an association is
linked. If you don't want an association linked you can do something
like

static mappedBy = [parent:'null']

Cheers

On Thu, Apr 26, 2012 at 9:24 AM, Luis Muniz <[hidden email]> wrote:
> Maybe to clarify the domain model, I used Burt Beckwith's GORM idioms to
> remove the collection in the hasMany part of the relationship.
>
> The conventional GORM implementation i guess should look something like
> this:
>
> class Mydomain3 {
>     String name
>
>     static belongsTo = [
>         parent: Mydomain3,
>         alias: Mydomain3,
>         foo: Mydomain3
>     ]
>     static hasMany = [
>         children: Mydomain3,
>         aliases: Mydomain3,
>         foos: Mydomain3
>     ]
>     static mappedBy = [
>         children: 'parent',
>         aliases: 'alias',
>         foos: 'foo'
>
>     ]
>
>     static constraints = {
>         parent(nullable: true)
>         alias(nullable: true)
>         foo(nullable: true)
>     }
> }
>
> The fact that the target of there are multiple relationships confuses GORM
> apparently, so that when you assign parent in objetc one, GORM assignes
> alias in object two.
>
>
>
>
> On Thu, Apr 26, 2012 at 9:02 AM, Luis Muniz <[hidden email]> wrote:
>>
>> bump.
>>
>> Revised example:
>>
>> class Mydomain {
>>
>>     String name
>>     Mydomain alias
>>     Mydomain
>>  parent
>>
>>     Mydomain foo
>>
>>     static constraints = {
>>
>>
>>         parent(nullable: true)
>>         alias(nullable: true)
>>
>>
>>         foo(nullable: true)
>>     }
>>
>> }
>>
>> 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 "============================"
>> }
>>
>>
>> Resulting in:
>>
>> one
>>
>> parent:null
>> alias:two
>> foo:null
>>
>> ============================
>> two
>> parent:one
>> alias:null
>>
>> foo:null
>> ============================
>>
>>
>>
>> On Wed, Apr 25, 2012 at 12:18 PM, Luis Muniz <[hidden email]> wrote:
>>>
>>> update, in grails 2.0.1 I have the same behaviour.
>>>
>>>
>>> On Wed, Apr 25, 2012 at 9:38 AM, Luis Muniz <[hidden email]> wrote:
>>>>
>>>> Sorry for crossposting (with stack exchange), but I feel this one is
>>>> justified, as it is a possible bug inside gorm, and in my opinion a
>>>> dangerous one.
>>>>
>>>> Feel free to educate me otherwise:
>>>>
>>>> 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
>>>>
>>>>     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 it.alias?.name
>>>>     println "============================"
>>>>
>>>> }
>>>>
>>>> I get the following result:
>>>>
>>>> one
>>>> two
>>>> ============================
>>>> two
>>>> 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
>>>>
>>>>
>>>
>>
>



--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: grails 1.3.7 - GORM misinterpretation of nullable reflective many-to-one relationship (possible bug?)

Graeme Rocher-4
Administrator
In reply to this post by ideasculptor
On Thu, Apr 26, 2012 at 12:07 PM, Samuel Gendler
<[hidden email]> wrote:

>
>
> On Thu, Apr 26, 2012 at 2:58 AM, Graeme Rocher <[hidden email]> wrote:
>>
>> Currently you use mappedBy to control whether an association is
>> linked. If you don't want an association linked you can do something
>> like
>>
>> static mappedBy = [parent:'null']
>
>
> He wants it linked.  The problem is that GORM is making assumptions about
> other links of the same type that are utterly invalid.  It is assuming that
> since  There is a property of the correct type on each side of the link,
> that it can assign things bidirectionally when that isn't actually the case.
> There are two properties of the same type, which also happens to be the type
> of the class being mapped.  Assigning a reference to object1 into the 2nd
> property of object2, causes the first property in object1 to be assigned to
> object2, which is just totally wrong.  It isn't a bidirectional relationship
> and assigning into one entity should have no impact on the other entity at
> all.  Gorm appears to be making an assumption about bidirectionality based
> on the existence of properties that match by type rather than an explicit
> mapping.

It is true GORM make some assumptions in terms of automatically
linking relationships, but the mapping I provided Luis will correct
those assumptions.

We can't change this behavior because it would be a massive breaking
change to existing applications that do expect relationships to be
automatically linked. It is an 80/20 case, 80% of the time the linking
happens as expected and everything is fine. 20% of the time you may
need to configure something

Cheers

>
>



--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: grails 1.3.7 - GORM misinterpretation of nullable reflective many-to-one relationship (possible bug?)

Mohamed Seifeddine
This sounds like the Flight, Airports example in the Grails reference docs. 


// Mo 

On Thu, Apr 26, 2012 at 12:34 PM, Graeme Rocher <[hidden email]> wrote:
On Thu, Apr 26, 2012 at 12:07 PM, Samuel Gendler
<[hidden email]> wrote:
>
>
> On Thu, Apr 26, 2012 at 2:58 AM, Graeme Rocher <[hidden email]> wrote:
>>
>> Currently you use mappedBy to control whether an association is
>> linked. If you don't want an association linked you can do something
>> like
>>
>> static mappedBy = [parent:'null']
>
>
> He wants it linked.  The problem is that GORM is making assumptions about
> other links of the same type that are utterly invalid.  It is assuming that
> since  There is a property of the correct type on each side of the link,
> that it can assign things bidirectionally when that isn't actually the case.
> There are two properties of the same type, which also happens to be the type
> of the class being mapped.  Assigning a reference to object1 into the 2nd
> property of object2, causes the first property in object1 to be assigned to
> object2, which is just totally wrong.  It isn't a bidirectional relationship
> and assigning into one entity should have no impact on the other entity at
> all.  Gorm appears to be making an assumption about bidirectionality based
> on the existence of properties that match by type rather than an explicit
> mapping.

It is true GORM make some assumptions in terms of automatically
linking relationships, but the mapping I provided Luis will correct
those assumptions.

We can't change this behavior because it would be a massive breaking
change to existing applications that do expect relationships to be
automatically linked. It is an 80/20 case, 80% of the time the linking
happens as expected and everything is fine. 20% of the time you may
need to configure something

Cheers

>
>



--
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware
http://www.springsource.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Loading...