Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails hasOne vs. belongsTo

To create one-to-one relationships in Grails I can do:

class Person {
    static hasOne = [address: Address]
}

In this case the Address table has the key to its person. I could also do:

class Address {
    static belongsTo = [person: Person]
} 

This gives the same result.

What is the difference between my two samples using hasOne and belongsTo?

like image 506
Sven Bauer Avatar asked Jul 01 '15 15:07

Sven Bauer


1 Answers

hasOne indicates that there is a bi-directional one-to-one relationship where the child table has the parent's foreign key, as in your example.

belongsTo is used to control cascades by indicating that the class belongs to the specified class. In your example, deleting a given Person would cascade the delete to any associated Addresses.

like image 80
doelleri Avatar answered Nov 11 '22 14:11

doelleri