Search code examples
grailsgrails-orm

An extra property makes Grails interpret relationship as bidirectional many-to-one


My real classes are different, so please bear with me for the limited sense of this example. Let's say I have 2 classes: Tree and Tool. A Tree has-many authorized Tools. But one of the Tools is a Rope which connects 2 trees, so Rope extends Tool and has a property endTree to store the other Tree:

class Tree { 
    static hasMany = [tools: Tool]
}

class Tool {
    // it doesn't matter here what the tool can do
}

class Rope extends Tool {
    Tree endTree
}

the problem I'm having is that Grails thinks this relation is bidirectional, so during evaluation of entities, isBidirectionalManyToOneWithListMapping() in AbstractGrailsDomainBinder returns true, in effect setting the endTree property to updateable=false, and hibernate skips endTree during save and update.

What am I doing wrong?

Thanks,

Mb


Solution

  • You can explicitly tell Grails not to treat a property as one side of a bidirectional relationship by using a mappedBy value of "none":

    class Tree { 
        static hasMany = [tools: Tool]
        static mappedBy = [tools: "none"]
    }
    

    and/or the same on the Rope end:

    class Rope extends Tool {
        Tree endTree
        static mappedBy = [endTree: "none"]
    }
    

    I'm not sure offhand whether you need the mappedBy on both ends or whether one or the other is sufficient.