History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: GRAILS-2832
Type: Bug Bug
Status: Open Open
Priority: Blocker Blocker
Assignee: Graeme Rocher
Reporter: James Cook
Votes: 4
Watchers: 4
Operations

If you were logged in you would be able to see more operations.
Grails

Setting Domain object updates first reference

Created: 16/Apr/08 04:29 PM   Updated: 14/Aug/08 06:35 PM
Component/s: Scaffolding
Affects Version/s: 1.0.2
Fix Version/s: 1.0.4

Time Tracking:
Not Specified

File Attachments: 1. Zip Archive bug.zip (204 kb)

Environment: Windows XP

Testcase included: yes


 Description  « Hide
There is a bug introduced in 1.0.2 where setting a child on a parent updates the first reference to that object:
Child.groovy
class Child {
    Parent parent
    static belongsTo = [page:Parent]
}
Parent.groovy
class Parent {
    Child child1
    Child child2
}
ParentTests.groovy
class ParentTests extends GroovyTestCase {
    void testStrangeBug() {
        def parent = new Parent()
        assertNull parent.child1
        parent.child2 = new Child(parent: parent)
        assertNotNull parent.child2
        assertNull parent.child1
    }
}

It does not matter how many children you have, setting any of them updates the first reference.



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
James Cook - 16/Apr/08 04:34 PM
Attached is a project that has the above classes, the test fails on the last line.

Lee Butts - 12/Jun/08 02:20 AM - edited
Grails is getting confused by the two relationships from Child to Parent, one is Child.parent and the other is Child.page (created implicitly by the belongsTo field).

It has tried to link the properties via a one-to-one mapping but is doing it incorrectly (Parent.child2 ends up not mapped as a relationship at all, yet Parent.child1 is) which is what is causing both fields on Parent to be set.

Was it intentional to have two relationships back to Parent from child? Your test will pass if you comment out either of the lines in Child.

cheers

Lee


James Cook - 12/Jun/08 07:54 AM
Minor mistake in the code. There should not be two references back from Child. It should read:

static belongsTo = [parent:Parent]

But it makes no difference - the test still fails. I could live without the declaration of Parent directly in Child, but commenting that line out made no difference to the test failing (given the above modification)


Lee Butts - 12/Jun/08 06:02 PM
Hi James,

apologies, you're right, that doesn't fix the test.

I think the best way to avoid problems is to remove the relationship back to Parent and make it a unidirectional one-to-one. Otherwise it becomes impossible for Grails to properly determine the property on Child that is the other side of Parent.child1 and Parent.child2

The other thing I tried which worked was to have two belongsTo links back to Parent e.g.

class Parent { static belongsTo = [parent1:Parent, parent2: Parent] }

However, I haven't checked to see if there are any other problems with this solution, or how Grails is linking the properties.

I'm not sure if there is a bug here or not, if you have two Child properties on Parent and one Parent property on Child how would you expect them to be linked?

cheers

Lee


Dániel Kamarás-Nagy - 14/Aug/08 06:35 PM