Grails hasOne vs.belongsTo [英] Grails hasOne vs. belongsTo

查看:25
本文介绍了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]
} 

这给出了相同的结果.

我使用 hasOnebelongsTo 的两个示例有什么区别?

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 会将删除级联到任何关联的 Addresses.

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 Addresses.

这篇关于Grails hasOne vs.belongsTo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆