`
boogie
  • 浏览: 235397 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类
最新评论

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}"
    }
} 
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics