class Book {
static hasMany = [chapterPageCounts: Integer]
static mapping = {
chapterPageCounts indexColumn: [name: "chapter_number", type: Integer],
joinTable: [column: "page_count"]
}
joinTable
目的
自訂用於單向一對多、多對多和基本集合型別的關聯表。
範例
基本集合型別
列舉集合型別
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']
}
}
多對多
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']
}
}
單向一對多
class Employee {
static hasMany = [projects: Project]
static mapping = {
projects joinTable: [name: 'EMP_PROJ',
column: 'PROJECT_ID',
key: 'EMPLOYEE_ID']
}
}
class Project { }
說明
用法:association_name(joinTable:map)
引數
-
name
- 表格名稱 -
key
(選用) - 外來鍵 -
column
(選用) - 反向欄位
GORM 有各種對應關聯型別的策略。其中一些,例如基本集合型別和多對多關聯,使用關聯表。您可以使用 joinTable
引數自訂如何建立此關聯表。