Search code examples
mappinggrails-orm

GORM two fields mapped with one column throw exception ...insert="false" update="false"


I have two classes, one that relate with the other (It is a One-to-One relationship). The two of them share the Primary Key.

One belongs to the other (belongsTo), the other has one parent (hasOne).

Something like this:

class Parent {
    int id
    static hasOne = [ child : Child ]         
}

class Child {
    int id
    static belongsTo = [ parent: Parent ]
    static mapping = {
        parent column: 'id'
    }
}

This is not working! :(


Solution

  • I found an answer, the HB error is pretty clear but in GORM the way you do that is different.

    The code will change slightly. Only the id can change, not the relationship therefor, you are able to tell Hibernate (and GORM) which one of the fields you care of inserting and updating.

    Note the mapping of Child

    class Parent {
        int id
        static hasOne = [ child : Child ]         
    }
    
    class Child {
        int id
        static belongsTo = [ parent: Parent ]
        static mapping = {
            parent column: 'id', insertable: false, updateable: false
        }
    }
    

    Hope this works for everyone. :)