(Quick Reference)

joinTable

Purpose

Customizes the join table used for undirectional one-to-many, many-to-many and primitive collection types.

Examples

Basic collection type:

class Book {

    static hasMany = [chapterPageCounts: Integer]

    static mapping = {
        chapterPageCounts indexColumn: [name: "chapter_number", type: Integer],
                          joinTable: [column: "page_count"]

}

Enum collection types:

enum VehicleStatus { OFF, IDLING, ACCELERATING, DECELERATING }
class Truck {

    static hasMany = [states: VehicleStatus]

    static mapping = {
        states joinTable: [name: 'VEHICLE_STATUS_LOG',
                           key: 'TRUCK_ID', column: 'STATUS']
    }
}

Many-to-many:

class Book {

    String title

    static belongsTo = Author

    static hasMany = [authors: Author]

    static mapping = {
        authors joinTable: [name: "mm_author_books", key: 'mm_book_id' ]
    }
}
class Author {

    String name

    static hasMany = [books: Book]

    static mapping = {
        books joinTable: [name: "mm_author_books", key: 'mm_author_id']
    }
}

Unidirectional One-to-many:

class Employee {

    static hasMany = [projects: Project]

    static mapping = {
        projects joinTable: [name: 'EMP_PROJ',
                             column: 'PROJECT_ID',
                             key: 'EMPLOYEE_ID']
    }
}
class Project {  }

Description

Usage: association_name(joinTable:map)

Arguments:

  • name - The table name

  • key (optional) - The foreign key

  • column (optional) - The inverse column

GORM has various strategies for mapping association types. Some of them, such as basic collection types and many-to-many associations, use a join table. You can customize how this join table is created using the joinTable argument.

For a bidirectional many-to-many, both sides share one join table that the owning side writes. When only the owning side configures a joinTable name, the inverse side uses the same table, schema and catalog. With Hibernate 7, the inverse side’s key is also the owner’s column and its column is the owner’s key; Hibernate 5 takes each side’s element column from the other side’s key, so there only the table, schema and catalog are inherited. An inverse side that configures its own joinTable name keeps its own configuration.