(快速參考)

hasOne

目的

定義兩個類別之間的雙向一對一關聯,其中外來鍵位於子類別中。

範例

class Face {
    ..
    static hasOne = [nose: Nose]
}
class Nose {
    Face face
}

在此範例中,我們定義 Face 類別和 Nose 類別之間的一對一關係

說明

使用 hasOne 關聯將外來鍵參考儲存在子表格中,而不是雙向一對一中的父類別。上面提供的範例會產生下列表格結構

create table face (id bigint generated by default as identity (start with 1),
                   version bigint not null,
                   primary key (id))
create table nose (id bigint generated by default as identity (start with 1),
                   version bigint not null,
                   face_id bigint not null,
                   primary key (id))

請注意外來鍵 face_id 儲存在 nose 表格中,而不是 face 表格中,就像沒有 belongsTo 的一般一對一定義。