---
title: "GORM many-to-many namimg convention"
author: "boogie"
date: "2008-06-10T11:50:05Z"
tags: []
slug: "gorm-many-to-many-namimg-convention"
draft: false
lastmod: "2008-06-10T11:50:05Z"
id: 201720
url: "https://boogie.iteye.com/blog/201720"
---

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