Grails hasOne vs.belongsTo [英] Grails hasOne vs. belongsTo
问题描述
要在 Grails 中创建一对一的关系,我可以这样做:
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]
}
这给出了相同的结果.
我使用 hasOne
和 belongsTo
的两个示例有什么区别?
What is the difference between my two samples using hasOne
and belongsTo
?
推荐答案
hasOne
表示存在双向的一对一关系,子表有父表的外键,如在你的例子中.
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
用于通过指示该类属于指定类来控制级联.在您的示例中,删除给定的 Person
会将删除级联到任何关联的 Address
es.
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 Address
es.
这篇关于Grails hasOne vs.belongsTo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!