"Fossies" - the Fresh Open Source Software Archive

Member "grails-core-5.2.4/grails-docs/src/test/resources/docs/ref/Domain Classes/hasOne.html" (14 Sep 2022, 60146 Bytes) of package /linux/www/grails-core-5.2.4.tar.gz:


Caution: In this restricted "Fossies" environment the current HTML page may not be correctly presentated and may have some non-functional links. You can here alternatively try to browse the pure source code or just view or download the uninterpreted raw source code. If the rendering is insufficient you may try to find and view the page on the grails-core-5.2.4.tar.gz project site itself.

(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

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.