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}"
    }
} 
 

你可能感兴趣的:(grails,groovy)