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
参数自定义此联结表的创建方式。