Grails
  1. Grails
  2. GRAILS-3126

Trees don't map correctly when properties are defined explicitly instead of generated by hasMany/belongsTo

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Cannot Reproduce
    • Affects Version/s: 1.0.3
    • Fix Version/s: 1.1.1
    • Component/s: None
    • Labels:
      None
    • Testcase included:
      yes

      Description

      It's impossible to map trees with Grails 1.0.3 because you can't allow NULL values for parent attribute of the root node(s).

      When this bug is fixed, I'm willing to extend the test project to check all kinds of operations on trees so this bug doesn't show up in any future version of Grails anymore.

        Activity

        Hide
        Aaron Digulla added a comment -

        It seems that the constraint "parent (nullable: true)" in TreeNode.groovy is ignored.

        Show
        Aaron Digulla added a comment - It seems that the constraint "parent (nullable: true)" in TreeNode.groovy is ignored.
        Hide
        Lee Butts added a comment -

        Hi Aaron,

        I'm not sure where the problem is that's causing those exceptions but as a workaround, the following domain class passes your test:

        class TreeNode {
        static hasMany = [ children: TreeNode ]
        static belongsTo = [ parent: TreeNode ]
        static constraints =

        { parent (nullable: true) }

        public TreeNode()

        { children = [] }

        }

        basically hasMany and belongsTo will create implicit properties 'parent' and 'children'. The constructor was needed to initialise children to an empty list (which I thought should have been done for you by Grails, which might help track down the problem)

        Show
        Lee Butts added a comment - Hi Aaron, I'm not sure where the problem is that's causing those exceptions but as a workaround, the following domain class passes your test: class TreeNode { static hasMany = [ children: TreeNode ] static belongsTo = [ parent: TreeNode ] static constraints = { parent (nullable: true) } public TreeNode() { children = [] } } basically hasMany and belongsTo will create implicit properties 'parent' and 'children'. The constructor was needed to initialise children to an empty list (which I thought should have been done for you by Grails, which might help track down the problem)
        Hide
        Aaron Digulla added a comment -

        I can confirm that; if either a field "parent" or "children" is defined in the class, the tests fail. As soon as only the automatic properties created by hasMany and belongsTo exist, the error goes away.

        I'll extend the test case with the workaround and try the original version with 1.0.4 as soon as the bug if fixed.

        Show
        Aaron Digulla added a comment - I can confirm that; if either a field "parent" or "children" is defined in the class, the tests fail. As soon as only the automatic properties created by hasMany and belongsTo exist, the error goes away. I'll extend the test case with the workaround and try the original version with 1.0.4 as soon as the bug if fixed.
        Hide
        Aaron Digulla added a comment -

        New version of TreeTest.zip which works.

        Uncomment the lines in TreeNode.groovy to activate the bug

        Show
        Aaron Digulla added a comment - New version of TreeTest.zip which works. Uncomment the lines in TreeNode.groovy to activate the bug
        Hide
        Aaron Digulla added a comment -

        "works" as in "more test cases" and "tests don't fail". There are a couple of comments in the test cases about stuff that doesn't work.

        Show
        Aaron Digulla added a comment - "works" as in "more test cases" and "tests don't fail". There are a couple of comments in the test cases about stuff that doesn't work.
        Hide
        Graeme Rocher added a comment -

        Seems this has been fixed already, since I cannot reproduce the problem with the attached example

        Show
        Graeme Rocher added a comment - Seems this has been fixed already, since I cannot reproduce the problem with the attached example

          People

          • Assignee:
            Graeme Rocher
            Reporter:
            Aaron Digulla
          • Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development