在普通的一对多映射中,"一"侧是该关联的所有者.为什么有人会使用belongsTo-mapping进行这样的映射?我错过了指定belongsTo的一些副作用吗?
换句话说:在GORM中指定belongsTo-mapping而不是指定它会产生什么影响?
是否指定belongsTo取决于所需的引用操作的类型.
如果你想让Grails做删除,CASCADE参考动作,那么DO指定belongsTo.如果你想让Grails做删除,RESTRICT参考动作,那么不要指定belongsTo.
例如
// "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. }
希望这可以帮助.