Grails: mapping column names of a field and a belongsTo of the same type

情到浓时终转凉″ 提交于 2019-12-05 15:24:01

Ok, finally I got it.

I used the "mappedBy" property.

http://grails.org/doc/2.1.0/ref/Domain%20Classes/mappedBy.html

The documentation (6.2.1.2 One-to-many) said that is for use in the "hasMany" side. That confused me because I didn't have a hasMany but rather two field of the same type in Class Amount. And what I really had to do was to use the mappedBy property, not in the Amount Class, but in the Bonification Class that has just one reference to Amount.

And with that I tell to the Amount Class, wich Bonification he has to back reference so "he" doesn't get confused, and take the bonificationRate field just as a field and not as a reference as I think he was doing before.

class Amount{

    String total //Total amount of something
    String type  //Type of amount, Dollars, %, Times something
    Bonification bonificationRate  //the amount can be "x times another bonification"

    static belongsTo = [parentBonification: Bonification]

    static mapping = {
       table "AMOUNTS"
       total column: "AMOU_TTOTAL"
       type column: "AMOU_TTYPE"
       parentBonification column: "BONI_CID"
       bonificationRate column: "BONI_TRATE_CID"
    }

}


class Bonification {  
    //among other things:  
    Amount amount

    static mappedBy = [amount: "parentBonification"]
}

That didn't created the parentBonification "BONI_CID" column but it Did created the bonificationRate "BONI_TRATE_CID" wich I needed.

Sorry if it's too messy. Thanks for the time and help anyway.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!