Grails hasOne vs. belongsTo [英] Grails hasOne vs. belongsTo
问题描述
class Person {
static hasOne = [地址:地址]
}
在这种情况下,地址表具有它的人。我也可以这样做:
class地址{
static belongsTo = [person:Person]
}
这给出了相同的结果。
我的两个样本使用 hasOne
和 belongsTo
?
hasOne
表示有一个双向一对一关系,其中子表具有父项的外键,如您的示例所示。
belongsTo
用于通过指示该类属于指定的类来控制级联。在你的例子中,删除给定的 Person
会将delete删除到任何关联的 Address
es。
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]
}
This gives the same result.
What is the difference between my two samples using hasOne
and belongsTo
?
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
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屋!