Grails / GORM: value belongs to a 1: N relationship

In the usual one-to-many mapping, one is the owner of the association. Why would anyone use the attribTo attribute to display this? Did I miss some side effect that determines my belonging to?

In other words: what are the effects of specifying the toto-mapping attribute in GORM and not specifying it?

+7
grails gorm
source share
2 answers

Whether ownership should be indicated depends on the type of reference action you want.

If you want Grails to do On Delete, CASCADE reference action, then DO indicate ownership. If you want Grails to do On Delete, the RESTRICT reference action, then DO NOT specify sortTo.

eg.

// "belongsTo" makes sense for me here. class Country { String name static hasMany = [states:State] } class State { String name; // I want all states to be deleted when a country is deleted. static belongsTo = Country } // Another example, belongsTo doesn't make sense here class Team { String name static hasMany = [players:Player] } class Player { String name // I want that a team should not be allowed to be deleted if it has any players, so no "belongsTo" here. } 

Hope this helps.

+19
source share

The belongsTo clause allows Grails to transparently cascade updates, saves, and deletes child objects. Without applyTo, if you try to delete the master record, you will receive a foreign key violation if it has any details that it has.

+2
source share

All Articles