grails hasOne vs direct member variable

Jeff Storey picture Jeff Storey · Oct 15, 2012 · Viewed 7.2k times · Source

Let's say I have a grails domain class that looks like

class Person {
    Address address
}

I could also declare it as

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

The second way would move the foreign key to the Address table rather than the person table.

What are the practical benefits (or disadvantages) of doing this one way vs the other? As far as I understand, they will both use foreign keys, it's just a matter of where the foreign key lives.

Answer

James Kleeh picture James Kleeh · Oct 15, 2012

If the foreign key exists on the address table, then that address can only have one person. If the foreign key is on the person table, multiple persons can have the same address.

It's not about what way is better/worse. It's about what is the correct way to model your data.