## 5.5.2.1 Table and Column Names

#### Table names

The database table name which the class maps to can be customized using a call to `table`:

    class Person {
      ..
      static mapping = {
          table 'people'
      }
    }

In this case the class would be mapped to a table called `people` instead of the default name of `person`.

#### Column names

It is also possible to customize the mapping for individual columns onto the database. For example if its the name you want to change you can do:

    class Person {
      String firstName
      static mapping = {
          table 'people'
          firstName column:'First_Name'
      }
    }

In this case we define method calls that match each property name (in this case `firstName`). We then use the named parameter `column`, to specify the column name to map onto.

#### Column type

GORM supports configuration of Hibernate types via the DSL using the type attribute. This includes specifing user types that subclass the Hibernate org.hibernate.usertype.UserType class, which allows complete customization of how a type is persisted. As an example if you had a `PostCodeType` you could use it as follows:

    class Address {
       String number
       String postCode
       static mapping = {
          postCode type:PostCodeType
       }
    }

Alternatively if you just wanted to map it to one of Hibernate's basic types other than the default chosen by Grails you could use:

    class Address {
       String number
       String postCode
       static mapping = {
          postCode type:'text'
       }
    }

This would make the `postCode` column map to the SQL TEXT or CLOB type depending on which database is being used.

See the Hibernate documentation regarding Basic Types for further information.

#### Many-to-One/One-to-One Mappings

In the case of associations it is also possible to change the foreign keys used to map associations. In the case of a many- or one-to-one association this is exactly the same as any regular column. For example consider the following:

    class Person {
        String firstName
        Address address    static mapping = {
            table 'people'
            firstName column:'First_Name'
            address column:'Person_Address_Id'
        }
    }

By default the `address` association would map to a foreign key column called `address_id`. By using the above mapping we have changed the name of the foreign key column to `Person_Adress_Id`.

#### One-to-Many Mapping

With a bidirectional one-to-many you can change the foreign key column used simple by changing the column name on the many side of the association as per the example in the previous section on one-to-one associations. However, with unidirectional association the foreign key needs to be specified on the association itself. For example given a unidirectional one-to-many relationship between `Person` and `Address` the following code will change the foreign key in the `address` table:

    class Person {
      String firstName
      static hasMany = [addresses:Address]
      static mapping = {
          table 'people'
          firstName column:'First_Name'
    	  addresses column:'Person_Address_Id'
      }
    }

If you don't want the column to be in the `address` table, but instead some intermediate join table you can use the `joinTable` parameter:

    class Person {
      String firstName
      static hasMany = [addresses:Address]
      static mapping = {
          table 'people'
          firstName column:'First_Name'
          addresses joinTable:[name:'Person_Addresses', key:'Person_Id', column:'Address_Id']
      }
    }

#### Many-to-Many Mapping

Grails, by default maps a many-to-many association using a join table. For example consider the below many-to-many association:

    class Group {
    	…
    	static hasMany = [people:Person]
    }
    class Person {
    	…
    	static belongsTo = Group
    	static hasMany = [groups:Group]
    }

In this case Grails will create a join table called `group_person` containing foreign keys called `person_id` and `group_id` referencing the `person` and `group` tables. If you need to change the column names you can specify a column within the mappings for each class.

    class Group {
       …
       static mapping = {
           people column:'Group_Person_Id'
       }
    }
    class Person {
       …
       static mapping = {
           groups column:'Group_Group_Id'
       }
    }

You can also specify the name of the join table to use:

    class Group {
       …
       static mapping = {
           people column:'Group_Person_Id',joinTable:'PERSON_GROUP_ASSOCIATIONS'
       }
    }
    class Person {
       …
       static mapping = {
           groups column:'Group_Group_Id',joinTable:'PERSON_GROUP_ASSOCIATIONS'
       }
    }