问题描述
在普通的一对多映射中,one是该协会的所有者。为什么会有人使用belongsTo-mapping进行这样的映射?我是否遗漏了指定belongsTo的一些副作用?
换句话说:在GORM中指定belongsTo-mapping与不指定它有什么影响?
是否指定 belongsTo 取决于。
如果您希望Grails执行Delete,CASCADE 引用操作,请指定 belongsTo 。如果您希望Grails在Delete,RESTRICT 引用操作上执行操作,请不要指定 belongsTo 。
例如。
//belongsTo在这里对我有意义。
class Country {
String name
static hasMany = [states:state]
}
class State {
String name;
//我想删除一个国家时删除所有州。
static belongsTo = Country
}
//另一个例子,belongsTo在这里没有意义
class Team {
字符串名称
static hasMany = [players:Player]
}
class Player {
字符串名称
//我希望一个团队不应该被允许被删除有任何球员,所以在这里没有belongsTo。
}
希望这会有所帮助。
In an ordinary one-to-many mapping the "one"-side is the owner of the association. Why would anyone use the belongsTo-mapping for such a mapping? Am I missing some side-effect of specifying belongsTo?
In other words: what are the effects of specifying a belongsTo-mapping in GORM vs. not specifying it?
Whether to specify belongsTo depends upon the type of referential action you want.
If you want Grails to do On Delete, CASCADE referential action, then DO specify belongsTo. If you want Grails to do On Delete, RESTRICT referential action, then DON'T specify belongsTo.
e.g.
// "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.
这篇关于Grails / GORM:1:N关系中belongsTo的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!