(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]
}
class Nose {
Face face
}
In this example we define a one-to-one relationship between the
Face
class and the
Nose
class
Description
A
hasOne
association should be used in the case where you want 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,
name varchar(255) 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,
shape varchar(255) 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
.