(Quick Reference)
hasOne
Purpose
Defines a bidirectional one-to-one association between two classes where the foreign key is in the child.
Examples
class Face {
..
static hasOne = [nose: Nose]
}
In this example we define a one-to-one relationship between the
Face
class and the
Nose
class
Description
Use a
hasOne
association to store the foreign key reference in child table instead of the parent in a bidirectional one-to-one. The example presented above will generate the following table structure:
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))
Notice that the foreign key
face_id
is stored in the
nose
table instead of the
face
table as with a normal one-to-one definition without
belongsTo
.