Permalink
Browse files

documented hasOne

  • Loading branch information...
graemerocher committed Jul 23, 2009
1 parent 34945e1 commit bd19ee4763948f0b8b02185fc5f8cb7523db249e
Showing with 37 additions and 0 deletions.
  1. +37 −0 grails-doc/src/ref/Domain Classes/hasOne.gdoc
@@ -0,0 +1,37 @@
+h1. hasOne
+
+h2. Purpose
+
+Defines a bidirectional one-to-one association between two classes where the foreign key is in the child.
+
+h2. Examples
+
+{code:java}
+class Face {
+ ..
+ static hasOne = [nose:Nose]
+}
+class Nose {
+ Face face
+}
+{code}
+
+In this example we define a one-to-one relationship between the @Face@ class and the @Nose@ class
+
+h2. 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:
+
+{code}
+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))
+{code}
+
+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@.

0 comments on commit bd19ee4

Please sign in to comment.