GORM many-to-many namimg convention

I have class named User and another one named Role and they are
related to each other in a many-to-many way, a user has many roles and
in a role there are many users

Besides the user and role tables I get another table named role_user
which has only two fields: roles_id and users_id

roles_id refers to the field id in the table user, and
users_id refers to the field id in the table role.

 

class Role {
    static hasMany = [users : User]
   
    String name
    Date dateCreated
    Date lastUpdated

    static mapping = {
        columns {
            users column:'roles_id'    
        }
    }

    static constraints = {
        name(blank:false)
        dateCreated()
        lastUpdated()
    }
   
    String toString() {
        "${id}-${this.name}"
    }
}

class User {
    static belongsTo = [Role]
    static hasMany = [roles : Role]

    String login
    Date dateCreated
    Date lastUpdated

    static mapping = {
        columns {
            roles column:'users_id'    
        }
    }

    static constraints = {
        login(length:6..30, unique:true)
        dateCreated()
        lastUpdated()
    }
   
    String toString() {
        "${id}-${this.login}"
    }
} 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值